Google Analytics

Sunday, November 20, 2016

Java 8 Locale date

Existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe and use package java.time

More info:
http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
http://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime

Example:
Current date time:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

LocalDateTime dt =  LocalDateTime.now();
//Format date time to String:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String entryDateTime = dt.format(formatter);

//Reverse formatting to LocalDateTime

LocalDateTime fromStartDateTime = LocalDateTime.parse(entryDateTime );
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String strCurrDateTime = LocalDateTime.now().format(formatter);
LocalDateTime toCurrentDateTime =   LocalDateTime.parse(strCurrDateTime);//now    



No comments:

Post a Comment