- How to get current date and/or time in Java ?
LocalDate.now() //Output: 2021-06-20
LocalTime.now() //Output: 23:22:17.939243900
LocalDateTime.now() //Output: 2021-06-20T23:13:37.193359900
ZonedDateTime.now() //Output: 2021-06-20T23:14:21.295180600-04:00[America/New_York]
- How to convert a String to date and time in Java ?
public static LocalDateTime StringToDate(){
String date = "01-October-2021 23:59:00.123";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy HH:mm:ss.SSS");
LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
return localDateTime;
}
For date strings with ‘T’ if you face the below exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-09-30T23:50:36.4259546' could not be parsed at index 10
add ‘T’ as shown in the below format:
public static void main(String args[]){
String now = "2021-09-30T23:50:36.4259546";
System.out.println(" " + LocalDateTime.parse(now,
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS")));
}
//OUTPUT: 2021-09-30T23:50:36.425954600
- How to create a Date Time instance with a previous date time value ?
Either you can parse the String as shown above. Or use the “of” factory method.
//of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
LocalDateTime.of(2021,10,31,23,59,59,283948576);
LocalDate.of(2021,10,31);
- How to specify milliseconds / microseconds / nanoseconds in DateTimeFormatter in Java ?
Formatter Symbol | Formatted examples. | |
Day of month | d/dd | 2/02 |
Month | M/MM/MMM/MMMM | 6/06/Jun/June |
Year | yy/yyyy | 21/2021 |
Hour in day(0-23) | H/HH | 2/02 , 14/14 |
Hour in am/pm (1-12) | h/hh | 4/04 |
Minues | m/mm | 9/09 |
Seconds | ss | 50 |
Milliseconds | SSS | 922 |
Microseconds | SSSSSS | 922600 |
Nanoseconds | n | 922600200 |
TimeZone | z / zzzz / VV | EDT / Eastern Daylight Time /America/New_York |
Offset | Z | -0400 |
Era | G/GGGG | AD/ Anno Domini |
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html
- Are Java8 Date/Time APIs thread safe ?
Yes. They are thread safe and that is one of main advantages of using Java8 Date/Time APIs against the APIs of previous java versions (Java7 and below).
- How to print date/time in a specific format ?
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy HH:mm:ss.n zzzz VV GGGG");
System.out.println(ZonedDateTime.now().format(dateTimeFormatter));
//Output: 20-June-2021 23:42:43.646265900 Eastern Daylight Time America/New_York Anno Domini
- How to convert a Date Time to a different Time Zone ?
ZonedDateTime preserves the Time Zone Data. If you want to change the timezone to a different time zone, use “withZoneSameInstant” api.
System.setProperty("user.timezone", "UTC");
ZonedDateTime zonedDateTimeUTC = ZonedDateTime.now();
System.out.println(zonedDateTimeUTC); //Output: 2021-06-27T03:44:06.031687100Z[UTC]
ZonedDateTime zonedDateTimeInEastern = zonedDateTimeUTC.withZoneSameInstant(ZoneId.of("US/Eastern"));
System.out.println(zonedDateTimeInEastern); //Output: 2021-06-26T23:44:06.031687100-04:00[US/Eastern]
List of all ZoneIds in java can be found here.
- How to get epoch milliseconds from LocalDateTime ? And how to convert milliseconds to LocalDateTime ?
public static void main(String args[]){
LocalDateTime ldt = LocalDateTime.of(2021, 8, 23, 11, 14, 10);
System.out.println(toMilliSeconds(ldt));
System.out.println(toLocalDateTime(1629731650000l));
}
public static long toMilliSeconds(LocalDateTime ldt){
return ldt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static LocalDateTime toLocalDateTime(long milliseconds){
return Instant.ofEpochMilli(milliseconds).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
// Output:
1629731650000
2021-08-23T11:14:10