Java从日输入获取即将到来的日期

Java从日输入获取即将到来的日期,java,date,simpledateformat,Java,Date,Simpledateformat,我有以下的意见 String day = "Tuesday"; SimpleDateFormat dayFormat = new SimpleDateFormat("E"); Date date1 = dayFormat.parse(day); 今天的日期是2012年10月19日。通过进入这一天,我想回到下一个即将到来的日期和时间。 如何将星期二转换为字符串,如下所示:2012-10-20 00:00 谢谢。下面是一个关于如何使用日历课程获取下周一的示例 Calenda

我有以下的意见

    String day = "Tuesday";
    SimpleDateFormat dayFormat = new SimpleDateFormat("E");
    Date date1 = dayFormat.parse(day);
今天的日期是2012年10月19日。通过进入这一天,我想回到下一个即将到来的日期和时间。 如何将星期二转换为字符串,如下所示:2012-10-20 00:00


谢谢。

下面是一个关于如何使用
日历
课程获取下周一的示例

Calendar now = Calendar.getInstance();  
int weekday = now.get(Calendar.DAY_OF_WEEK);  
if (weekday != Calendar.MONDAY)  
{  
    // calculate how much to add  
    // the 2 is the difference between Saturday and Monday  
    int days = (Calendar.SATURDAY - weekday + 2) % 7;  
    now.add(Calendar.DAY_OF_YEAR, days);  
}  
// now is the date you want  
Date date = now.getTime();  
String format = new SimpleDateFormat(...).format(date);
发件人:


更多信息:

下面是一个关于如何使用
日历
类获取下周一的示例

Calendar now = Calendar.getInstance();  
int weekday = now.get(Calendar.DAY_OF_WEEK);  
if (weekday != Calendar.MONDAY)  
{  
    // calculate how much to add  
    // the 2 is the difference between Saturday and Monday  
    int days = (Calendar.SATURDAY - weekday + 2) % 7;  
    now.add(Calendar.DAY_OF_YEAR, days);  
}  
// now is the date you want  
Date date = now.getTime();  
String format = new SimpleDateFormat(...).format(date);
发件人:

更多信息:

您可以使用它进行简单的日期操作。例如:

Calendar calendar = Calendar.getInstance(); //gets a localized Calendar instance
calendar.setTime(date1);                    //sets the Calendar time to your date
calendar.add(Calendar.DATE, 1);             //adds 1 day
Date date2 = calendar.getTime();            //gets the resulting date
可以使用进行简单的日期操作。例如:

Calendar calendar = Calendar.getInstance(); //gets a localized Calendar instance
calendar.setTime(date1);                    //sets the Calendar time to your date
calendar.add(Calendar.DATE, 1);             //adds 1 day
Date date2 = calendar.getTime();            //gets the resulting date

使用
日历
API,如下所示-

    String day = "Tue";
    SimpleDateFormat dayFormat = new SimpleDateFormat("EEE");

    Date date1 = dayFormat.parse(day);        
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1); 

    //just keep adding a day to current date until the day of week is same
    Calendar cal = Calendar.getInstance();        
    while(cal.get(Calendar.DAY_OF_WEEK) != cal1.get(Calendar.DAY_OF_WEEK)) {
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }

    System.out.println(cal.getTime());
输出:

2012年10月23日星期二22:34:25 CDT


使用
日历
API,如下所示-

    String day = "Tue";
    SimpleDateFormat dayFormat = new SimpleDateFormat("EEE");

    Date date1 = dayFormat.parse(day);        
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1); 

    //just keep adding a day to current date until the day of week is same
    Calendar cal = Calendar.getInstance();        
    while(cal.get(Calendar.DAY_OF_WEEK) != cal1.get(Calendar.DAY_OF_WEEK)) {
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }

    System.out.println(cal.getTime());
输出:

2012年10月23日星期二22:34:25 CDT


很好的谷歌搜索-你想自己添加什么?很好的谷歌搜索-你想自己添加什么?