DateTime API in JAVA 8 – TimeStamp and Time Operations

In this article, we’ll share some basic DateTime conversion by using DateTime API of Java 8 that you would need in a project. Working with Dates, trying to convert them into the proper format is always a pain in the ass. After Java 8, a new DateTime API is introduced.

We’ll basically work on examples.

Get Current TimeStamp as Ms or Sec

You need to use Instant object to get the timestamp. You can get the timestamp as milliseconds or second.

Instant instant = Instant.now();
long timeStampMillis = instant.toEpochMilli();
System.out.println(timeStampMillis);

Output: 1549057795923

Instant instant = Instant.now();
long timeStampMillis = instant.getEpochSecond();
System.out.println(timeStampMillis);

Output: 1549057795

Get Local Date by TimeZone

You can use LocalDate object to get the current system date. You are able to pass ZoneId object to change the time zone.

LocalDate localDate = LocalDate.now(ZoneId.of("GMT+02:00"));
System.out.println("GMT+2 " + localDate.toString());
localDate = LocalDate.now(ZoneId.of("GMT+05:00"));
System.out.println("GMT+5 " + localDate.toString());

Output:

According to the hour that you run that code, you might have the same result or a different one. As a one-time zone might have started to live the next already :)

GMT+2 2019-02-01
GMT+5 2019-02-02

Get X Days/Months/Year Early or Later

There are very handy methods coming with LocalDate object. You can add, subtract days, months, years or even week to a given date and get the new date object.

LocalDate date = LocalDate.now(ZoneId.of("GMT+02:00"));
System.out.println("Today " + date.toString());
LocalDate earlyDay = date.minusDays(3);
System.out.println("Past " + earlyDay.toString());
LocalDate futureDay = date.plusDays(3);
System.out.println("Future "+futureDay.toString());

Output:

Today 2019-02-02
Past 2019-01-30
Future 2019-02-05

LocalDate date = LocalDate.now(ZoneId.of("GMT+02:00"));
System.out.println("Today " + date.toString());
LocalDate earlyMonth = date.minusMonths(3);
System.out.println("Past " + earlyMonth.toString());
LocalDate futureMonth = date.plusMonths(3);
System.out.println("Future "+futureMonth.toString());

Output:

Today 2019-02-02
Past 2018-11-02
Future 2019-05-02

You can also add or subtract week or year with other methods.

LocalDateTime to TimeStamp

You might need to convert a date object to timestamp. Here’s how you can achieve this challenge.

LocalDate date = LocalDate.now(ZoneId.of("GMT+02:00"));
LocalDate earlyDay = date.minusDays(dayCount);
LocalDateTime dateTime = earlyDay.atStartOfDay();
Timestamp t = Timestamp.valueOf(dateTime);
System.out.println("TimeStamp is "+t.getTime());

Output: TimeStamp is 1549882173000

DateTime Formatting

String FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
LocalDateTime date = LocalDateTime.now(ZoneId.of("GMT+02:00"));
System.out.println("Today " + date.format(DateTimeFormatter.ofPattern(FORMAT)));

Output: Today 2019-02-02 08:55:47.208

Example Formats:

yyyy-MM-dd           (2009-11-30)

dd-MM-YYYY           (31-11-2008)
    
yyyy-MM-dd HH:mm:ss  (2009-08-30 22:58:59)

HH:mm:ss.SSS         (22:58.59.999)

yyyy-MM-dd HH:mm:ss.SSS   (2009-11-30 22:58:59.999)

yyyy-MM-dd HH:mm:ss.SSS Z   (2009-11-30 22:58:59.999 +0100)
y   = year   (yy or yyyy)
M   = month  (MM)
d   = day in month (dd)
h   = hour (0-12)  (hh)
H   = hour (0-23)  (HH)
m   = minute in hour (mm)
s   = seconds (ss)
S   = milliseconds (SSS)
z   = time zone  text        (e.g. Pacific Standard Time...)
Z   = time zone, time offset (e.g. -0800)

TimeStamp to DateTime 

String FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
Instant instant = Instant.now();
long timeStampMillis = instant.toEpochMilli();
ZoneId zone = ZoneId.systemDefault();
DateTimeFormatter df = DateTimeFormatter.ofPattern(FORMAT).withZone(zone);
String time = df.format(Instant.ofEpochMilli(timeStampMillis));
System.out.println("Formatted Date " + time);

Output: Formatted Date 2019-02-02 09:33:33.084

String to Date

String dateInString = "26 Aug 1985";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM yyyy", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse(dateInString, formatter);
System.out.println("Parsed Date " +dateTime.toString());

Output: Parsed Date 1985-08-26

String to TimeStamp

String timestampString = "2018-11-27T16:30:00.21234Z";
Instant inst = Instant.parse(timestampString);
System.out.println("String to TimeStamp "+inst.toEpochMilli());

Output: String to TimeStamp 1543336200212

Hope those will help when you need to deal with Dates. In case you encounter any problems and questions, just let us know by commenting so we add new examples.

Happy Testing!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.