-3

What's a clean, modern way of formatting Java Date and Time from System.currentTimeInMillis() so that the formatting is as follows:

  • For Date: YYYY-MM-DD
  • For Time: HH-MM-SS-mmmm, where HH is a 2-digit hour, MM is a 2-digit minute, SS is a 2-digit second, and mmm is a 3-digit millisecond

Looking to get the formatting Date/Time of the current instant (now). Something like:

long current = System.currentTimeInMillis();
String formattedDate = getFormattedDateFromCurrent(current);
String formattedTime = getFormattedTimeFromCurrent(current);

Don't know if raw Java can do this, or if I have to switch over to something like joda-time. Thanks in advance!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    Have you tried SimpleDateFormat? Does it not work for this? – Hovercraft Full Of Eels Sep 22 '12 at 12:22
  • 1
    Seriously? The class to use comes as first result when googling for "format date in Java" – JB Nizet Sep 22 '12 at 12:22
  • I thought about using `SimpleDateFormat` but it doesn't handle time down to milliseconds which is why I thought I might need to switch over to `joda-time`. – IAmYourFaja Sep 22 '12 at 12:28
  • 2
    `"I thought about using SimpleDateFormat but it doesn't handle time down to milliseconds"` -- *really*? Are you absolutely sure? Have you reviewed the API? – Hovercraft Full Of Eels Sep 22 '12 at 12:30
  • I just checked the API, and I see that I was wrong and it does handle milliseconds, however I can only use ":" as the delimiter between the different date/time segments, whereas here I absolutely *need* to use "-", which is what the client is expecting. – IAmYourFaja Sep 22 '12 at 12:48
  • 2
    And where did you come up with *that* assumption? Did you actually *try* it? – Dylan Sep 22 '12 at 12:49
  • Look at the "Examples" section on [this](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html) page - they clearly denote that "-" isn't supported, only ":" or concatenating all the segments together. Also, @HovercraftFullOfEels - I am here to learn and do not profess to be an expert in anything. If I have asked a strange question, it's because I don't understand it and want to learn better. It hurts my feelings when you describe my question as a "*car accident in slow motion*". – IAmYourFaja Sep 22 '12 at 14:08
  • `"Look at the "Examples" section on this page - they clearly denote that "-" isn't supported, only ":" or concatenating all the segments together."` -- Please indicate where you see this documented as I cant find it in the API. Previous comment deleted, and please accept my apology. – Hovercraft Full Of Eels Sep 22 '12 at 14:19
  • OK, well, I suppose that you've either solved this and not informed us or have lost interest in this and not informed us. – Hovercraft Full Of Eels Sep 23 '12 at 17:46
  • Check out http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – sharadendu sinha Sep 22 '12 at 12:26

3 Answers3

0

You can just pass the long value to the Date constructor and it will give you the date.

You can then format this date to get you desired format.

Code to convert to date -

long tmp = 1018021774496l;  
Date d = new Date(tmp);  
System.out.println(d); 

Have a look here. Its the first result in Google ;)

JHS
  • 7,761
  • 2
  • 29
  • 53
0

SimpleDateFormat is a common tool for formatting date/time in Java. You just need to specify the pattern that you want to represent the date/time.

Date now = new Date();
// or you can use
// long millis = System.currentTimeInMillis();
// Date now = new Date(millis);

String datePattern = "yyyy-mm-dd HH-MM-SS";
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
String formattedDate = formatter.format(now);
Genzer
  • 2,921
  • 2
  • 25
  • 38
0

tl;dr

OffsetDateTime.now( ZoneOffset.UTC ).toLocalDate().toString()

2016-01-23

…and…

OffsetDateTime.now( ZoneOffset.UTC ).toLocalTime().toString()

12:34:56.789

Using java.time

The “clean, modern way” would be using java.time classes.

No need to call System.currentTimeInMillis(). The java.time classes can capture the current moment.

If you want the current moment in UTC, use an Instant.

Instant instant = Instant.now();

For formatting, morph that into the more flexible OffsetDateTime.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );

To generate strings, you could define a DateTimeFormatter object. But in your case, the desired output happens to conform to the standard ISO 8601 formats used by default in the java.time classes.

So, for the date-only portion, use a LocalDate. For time-of-day portion, LocalTime. Call their toString methods.

String dateOnly = odt.toLocalDate().toString();
String timeOnly = odt.toLocalTime().toString();

Resolution

Note that while java.time classes resolve to nanoseconds, capturing the current moment in Java 8 is limited to milliseconds. In Java 9, a new implementation of Clock provides capturing the current moment in nanoseconds (or as best your computer clock hardware can provide).


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154