Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human readable and parsable date format in Java

Tags:

I want to save a Date object to a readable string (for example 22/10/2009 21:13:14) that is also parsable back to a Date object.

I have tried many things and the best I could find was to use DateFormater for parsing and formating but it has a setback. When you format a date you lose seconds information. I tried to find if there is an option to format it and display the seconds (even better would be to the millisecond level since that's the resolution the Date object allows you to have) but I came up short.

Any ideas?

like image 278
Savvas Dalkitsis Avatar asked Sep 23 '09 20:09

Savvas Dalkitsis


People also ask

How do you format a date in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.

How do you check if the date is in YYYY MM DD format in Java?

DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019"));

What is Java Util date format?

Date format conversion yyyy-mm-dd to mm-dd-yyyy.

What is datatype of date in Java?

The Date in Java is not only a data type, like int or float, but a class. This means it has its own methods available for use. A Date in Java also includes the time, the year, the name of the day of the week, and the time zone. One of its methods includes extracting the time from the Date object.


1 Answers

Take a look at java.text.SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date dt = new Date();
String S = sdf.format(dt); // formats to 09/23/2009 13:53:28.238
Date dt2 = sdf.parse(S); // parses back
like image 114
ChssPly76 Avatar answered Sep 30 '22 22:09

ChssPly76