简体   繁体   中英

How can I get Unix Timestamp automatically for every next day?

I need to generate Unix timestamp in milliseconds for every next day at a definite time.

For example: today is 4/11/2021 at 09:00:00 then timestamp is: 1636002000000

for tomorrow I need 5/11/2021 at 09:00:00

day after tomorrow 6/11/2021 at 09:00:00

and so on...

so how can I get auto generated timestamp for same in Java?

In Linux you would get Chron to start a class that could write somewhere then exit after checking the stream was written. You would ask it to start around 30 seconds earlier and the class look for the moment to write to file then exit itself. With java the java,time.Clock requires to be UTC for a UNIX timestamp.

you would use pieces like this in the class

// static Clock fixed(Instant fixedInstant, ZoneId zone)  note Clock.Instant  

Clock   uxtmptmp = Clock.systemUTC();

// not sure of the behaviour of java.time.Clock.tick()   tick(Clock baseClock, Duration tickDuration) - note Clock.millis()   

Instant instxstmp = Instant.now(uxtmptmp);
//...
long uxepo = instxstmp.getEpochSecond();

I got my solution:

First I get the current date , month, and year using

Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH)+1;
        int Year = calendar.get(Calendar.YEAR);

Second use a StringBuilder where I fixed 09:00 Am

StringBuilder s1 = new StringBuilder().append(day).append("/")
                .append(month).append("/").append(Year).append(" ")
                .append("09").append(":").append("00");

Third parse it and get timeStamp:

todayTimeStamp = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(String.valueOf(s1)).getTime();

java.time

Like Samuel Marchant I recommend that you use java.time, the modern Java date and time API, for your date and time work.

If you want every day at 9, define that as a constant first:

private static final LocalTime TIME = LocalTime.of(9, 0);

Now your milliseconds values can be obtained in this way:

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault()).with(TIME);
    for (int i = 0; i < 10; i++) {
        long timestampMillis = zdt.toInstant().toEpochMilli();
        System.out.format("%-30s %13d%n", zdt, timestampMillis);

        zdt = zdt.plusDays(1);
    }

Output when I ran the code just now:

 2021-11-11T09:00+01:00[Europe/Paris] 1636617600000 2021-11-12T09:00+01:00[Europe/Paris] 1636704000000 2021-11-13T09:00+01:00[Europe/Paris] 1636790400000 2021-11-14T09:00+01:00[Europe/Paris] 1636876800000 2021-11-15T09:00+01:00[Europe/Paris] 1636963200000 2021-11-16T09:00+01:00[Europe/Paris] 1637049600000 2021-11-17T09:00+01:00[Europe/Paris] 1637136000000 2021-11-18T09:00+01:00[Europe/Paris] 1637222400000 2021-11-19T09:00+01:00[Europe/Paris] 1637308800000 2021-11-20T09:00+01:00[Europe/Paris] 1637395200000

Please enjoy not only how much simpler but first and foremost how much more natural to read the code is compared to the code in your own answer. This is typical for java.time compared to the old and poorly designed classes from Java 1.0 and 1.1.

Link

Oracle Tutorial: Date Time explaining how to use java.time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM