Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

getting milliseconds from localDatetime in java 8

Program

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class LocalDateTimeMillisecond {
    public static void main(String[] args) {
  
        LocalDateTime localDateTime = LocalDateTime.now();
  
        ZonedDateTime zdt = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
        long date = zdt.toInstant().toEpochMilli();
  
        System.out.println(date);
    }
}

Output

1584458975530

Description

public static LocalDateTime now()

Obtains the Current date-time from the system Clock in the default time-zone.
This will query the system clock in the default time-zone to obtain the current date-time.

Obtains the current date-time from the system clock in the default time-zone.
This will query the system clock in the default time-zone to obtain the current date-time.

Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.

Returns:

the current date-time using the system clock and default time-zone, not null

public static ZonedDateTime of(LocalDateTime localDateTime,
ZoneId zone)

Obtains an instance of ZonedDateTime from a local date-time.
This creates a zoned date-time matching the input local date-time as closely as possible. Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may be adjusted.

The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid offset from UTC/Greenwich for the local date-time as defined by the rules of the zone ID.

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to “summer”.

In the case of a gap, when clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one-hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to “summer”.

Parameters:

localDateTime – the local date-time, not null
zone – the time-zone, not null

Returns:

the zoned date-time, not null

public long toEpochMilli()

Converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
If this instant represents a point on the time-line too far in the future or past to fit in long milliseconds, then an exception is thrown.

If this instant has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.

Returns:

the number of milliseconds since the epoch of 1970-01-01T00:00:00Z

The post getting milliseconds from localDatetime in java 8 appeared first on Candidjava -Core Java, Servlet, Jsp, Hibernate,Spring,.



This post first appeared on Core Java,Servlet,Jsp,Struts,Hibernate,Spring Fram, please read the originial post: here

Share the post

getting milliseconds from localDatetime in java 8

×

Subscribe to Core Java,servlet,jsp,struts,hibernate,spring Fram

Get updates delivered right to your inbox!

Thank you for your subscription

×