次の入力があります
String day = "Tuesday";
SimpleDateFormat dayFormat = new SimpleDateFormat("E");
Date date1 = dayFormat.parse(day);
そして今日の日付は 2012-10-19 です。曜日を入力することで、次の日時を取得したい。次のように火曜日を文字列に変換するにはどうすればよいですか: 2012-10-20 00:00?
ありがとうございました。
次の入力があります
String day = "Tuesday";
SimpleDateFormat dayFormat = new SimpleDateFormat("E");
Date date1 = dayFormat.parse(day);
そして今日の日付は 2012-10-19 です。曜日を入力することで、次の日時を取得したい。次のように火曜日を文字列に変換するにはどうすればよいですか: 2012-10-20 00:00?
ありがとうございました。
Calendarクラスを使用して次の月曜日を取得する方法の例を次に示します。
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);
から: http://www.coderanch.com/t/385117/java/java/date-next-Monday
詳細: http://www.java2s.com/Code/Java/Data-Type/GetNextMonday.htm
You can use Calendar for simple date manipulation. For example:
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次のように 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());
出力:
火曜日 10 月 23 日 22:34:25 CDT 2012