Sunday 12 January 2020

Get milliseconds from LocalDateTime


Approach 1: Using LocalDateTime and ZonedDateTime
LocalDateTime localDateTime = LocalDateTime.of(2019, 12, 24, 10, 37, 53);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("America/Los_Angeles"));
long millis = zonedDateTime.toInstant().toEpochMilli();

App.java
package com.sample.app;

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

public class App {

 public static void main(String args[]) {

  LocalDateTime localDateTime = LocalDateTime.of(2019, 12, 24, 10, 37, 53);
  ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("America/Los_Angeles"));

  long millis = zonedDateTime.toInstant().toEpochMilli();

  System.out.println(millis);
 }

}

Approach 2: Using 'Timestamp.valueOf' function
LocalDateTime localDateTime = LocalDateTime.of(2019, 12, 24, 10, 37, 53);
long milliSeconds = Timestamp.valueOf(localDateTime).getTime();

App.java
package com.sample.app;

import java.sql.Timestamp;
import java.time.LocalDateTime;

public class App {

 public static void main(String args[]) {

  LocalDateTime localDateTime = LocalDateTime.of(2019, 12, 24, 10, 37, 53);
  long milliSeconds = Timestamp.valueOf(localDateTime).getTime();

  System.out.println(milliSeconds);
 }

}


You may like
Previous                                                    Next                                                    Home

No comments:

Post a Comment