How can I analyze the day of the week and time for the next logical date from today?

I have a date in a string that looks like MON 07:15. I am trying to parse this on Dateusing this piece of code:

System.out.println(new SimpleDateFormat("E kk:mm").parse("MON 07:15"));

Using the code above, prints a date that states:

Mon Jan 05 07:15:00 EET 1970

I would like to parse a date string for the next upcoming date. At the time of publication, my local date and time Fri Aug 08 11:45:00 EEST 2014, and the next Monday will be on the 11th, so the final date I'm looking for is Mon Aug 11 07:15:00 EEST 2014. How can I make it out?


The object of the day and time that I will understand will always be in the future.

+4
source share
3 answers

I would separate parsing from everything else.

, , .

, - . .

, , /. :

  • ?
  • 06:00 , 07:15, ?
  • - 08:00 , 07:15, ?

, : . - , ", " - " /" " /",.

java.time Java 8 Joda Time - , API java.util.*.

+7

- ?

 DateFormat df = new SimpleDateFormat("E kk:mm");
 Date date = df.parse("MON 07:15");
 Date today = new Date();
 Calendar calendar = Calendar.getInstance();
 Calendar calendar1 = Calendar.getInstance();
 calendar.setTime(today);
 calendar1.setTime(date);
 if (calendar.get(Calendar.DAY_OF_WEEK) == calendar1.get(Calendar.DAY_OF_WEEK)) {
       String time = df.format(today);
       Date t1 = df.parse(time);
        if (t1.before(date)) {
            calendar.set(Calendar.HOUR, calendar1.get(Calendar.HOUR));
            calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
            calendar.set(Calendar.SECOND, 0);
            System.out.println(calendar.getTime());
        } else {
            calendar.add(Calendar.DATE, 7);
            calendar.set(Calendar.HOUR_OF_DAY, calendar1.get(Calendar.HOUR));
            calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
            calendar.set(Calendar.SECOND, 0);
            System.out.println(calendar.getTime());
        }
    } else {
        int toDay = calendar.get(Calendar.DAY_OF_WEEK);
        int givenDay = calendar1.get(Calendar.DAY_OF_WEEK);
        int count = 7 - toDay + givenDay;
        calendar.add(Calendar.DAY_OF_MONTH, count);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.HOUR_OF_DAY, calendar1.get(Calendar.HOUR));
        calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
        System.out.println(calendar.getTime());
    }

:

   Mon Aug 11 07:15:00 IST 2014

, , .

0

, .

.Date/.Calendar

java.util.Date .Calendar Java, , . .

Joda-Time java.time

. Java :

. , 3 4 .

, JVM . sys - - Java- JVM setDefault .

Joda-Time 2.7.

, /. UTC, DateTimeZone.UTC.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );

. .

DateTime dateTime = DateTime.now( zone );

, . , Joda-Time # 1 , , java.util.Calendar. - , ( , ).

int dayOfWeek = DateTimeConstants.SATURDAY;

withDayOfWeek . (?:), , , .

DateTime future = ( dateTime.getDayOfWeek() < dayOfWeek )
        ? dateTime.withDayOfWeek( dayOfWeek )
        : dateTime.plusWeeks( 1 ).withDayOfWeek( dayOfWeek );

You can adjust the time of day at the first moment of the day to emphasize the focus on the day, rather than a specific moment during the day.

future = future.withTimeAtStartOfDay(); // Adjust time-of-day to first moment of the day to stress the focus on the entire day rather than a specific moment within the day. Or use `LocalDate` class.

Dump for the console.

System.out.println( "Next day # " + dayOfWeek + " after " + dateTime + " is " + future );

At startup.

Next day # 6 after 2015-04-18T16:03:36.146-04:00 is 2015-04-25T00:00:00.000-04:00
0
source

All Articles