Get milliseconds by next Saturday

I am developing a weekly event, but I need to get milliseconds (unix timestamp) by next Saturday. How can i do this?

+3


source to share


3 answers


1 create a calendar

 Calendar calNow = Calendar.getInstance();

      

2 create another calendar, set it at midnight and move from day to day until you hit Saturday



    Calendar calNextSat = Calendar.getInstance();
    calNextSat.set(Calendar.HOUR, 0);
    calNextSat.set(Calendar.MINUTE, 0);
    calNextSat.set(Calendar.SECOND, 0);
    while(calNextSat.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY){
        calNextSat.add(Calendar.DATE, 1);
    }

    System.out.println(calNextSat.getTimeInMillis() - calNow.getTimeInMillis());

      

process the script if already on Saturday you get <= 0 result

+4


source


I am developing a weekly event

Using milliseconds for this kind of date / time tracking will probably mislead you. For example, due to Daylight Saving Time (DST) and other anomalies, a day is not always 24 hours long, so a week is not always ((1000L * 60 * 60 * 24) * 7) milliseconds.

Joda-Time or java.time

I suggest learning how to use a complex date library. In Java, this means:

Timezone

The time zone is critical in determining the day and day of the week. Use the correct time zone names , not 3 or 4 letter codes.

Sample code to get the next day of the week

Here is some sample code using Joda-Time 2.7.

Get the timezone you want / expect. If you are working in UTC , use a constant DateTimeZone.UTC

.

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

      

Get the time you want. Here I am using the current moment.

DateTime dateTime = DateTime.now( zone );

      

Indicate the future day of the week that you want. Note that Joda-Time uses a sensible # 1 number for the first day of the week, not the zero-based count found in java.util.Calendar. The first day of the week is Monday, by international norms and standards (not Sunday, as is usual in the United States).

int dayOfWeek = DateTimeConstants.SATURDAY;

      

The team withDayOfWeek

can go back in time. Therefore, we use the thermal operator ( ?:

)
to make sure we go ahead on time, adding the week as needed.

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 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 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

      

Until

The above code gives us the desired future point in time (next Saturday).

If all you really need is the number of milliseconds between time and then, subtract from each of them the internal count — from the epoch in milliseconds. Note the use of 64-bit long

rather than 32-bit int

when tracking in milliseconds.

long elapsedMilliseconds = ( future.getMillis() - dateTime.getMillis() );

      

Note that if you are doing this work in java.time, but rather in Joda-Time, keep in mind that internally java.time uses nanoseconds , not milliseconds . As I recall, you can find millisecond methods. Or divide nanoseconds by a million (yes, a million is not thousands, since microseconds are in between.

You might want to think more reasonably about the time span between us and now. Joad-Time offers three classes for representing a time span in different ways:

Sample code, again Joda-Time 2.7.

Interval interval = new Interval( dateTime , future );
Period period = interval.toPeriod();

      

Dump for console.

System.out.println( "interval: " + interval );
System.out.println( "period: " + period );

      

At startup.

interval: 2015-04-18T16:17:45.109-04:00/2015-04-25T00:00:00.000-04:00
period: P6DT7H42M14.891S

      

Note the default String format used for the Period value. This format is standard, part of ISO 8601 , referred to Duration

in their terminology. This format PnYnMnDTnHnMnS

, where P

marks the beginning, and T

separates the part of the date from the time part.

Both Joda-Time and java.time use ISO 8601 as their default to parse and generate string representations of date and time values.

+3


source


I would use Calendar

to get the current day of the week. And you could subtract it like

public static int daysUntilSaturday(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return 7 - cal.get(Calendar.DAY_OF_WEEK);
}

      

Then you can do some simple arithmetic like

/*
 * 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in
 * an hour, 24 hours in a day.
 */
final long millisecondsPerDay = 1000L * 60 * 60 * 24;
System.out.println((daysUntilSaturday(new Date()) * millisecondsPerDay)
        + System.currentTimeMillis());

      

+2


source







All Articles