Question

I need to be able to save and load the current time in java. I could use System.currentTimeMillis() to store it in a long, but I also need to be able to write it out in different formats, like; "yyyy-mm-dd", "dd/mm hour:min:sec", and such.

The program will save the time I got from System.currentTimeMillis() into a txt file, so even if something happens to the computer or program it needs to be able to just go right back to it's task.

Was it helpful?

Solution 3

You can use code similar to the following

String formatted = new SimpleDateFormat("yyyy-mm-dd").format(new Date(System.currentTimeMillis()));

OTHER TIPS

tl;dr

Serializing

Serializing to text:

java.time.Instant.now().toString()

2018-01-01T01:23:45.123456789Z

Instantiate from text:

Instant.parse( “2018-01-01T01:23:45.123456789Z” )

Formatted strings

Adjust into time zone:

instant.atZone( ZoneId.of( “Africa/Tunis” ) )  // Instantiate a `ZonedDateTime` object 

Generate strings in other formats:

DateTimeFormatter.ofPattern( … )

…or, better:

DateTimeFormatter.ofLocalized…

…then:

myZonedDateTime.format( formatter )

Track Date-Time Values, Not Milliseconds

Generally speaking, tracking date-time values by millisecond-since-epoch is tricky business and should be avoided. The values are meaningless when read by humans. Different software uses different numbers (seconds versus milliseconds versus nanoseconds). Different software uses different epochs (not always the beginning of 1970 as you may be expecting). Tracking by date-time values by milliseconds like trying to track text by bits rather than using String, FileReader, and FileWriter objects. We have good date-time libraries, so use them.

Joda-Time | java.time

By good date-time libraries, I am referring to Joda-Time or the new java.time package in Java 8. Avoid the older bundled classes, java.util.Date & .Calendar, as they are notoriously troublesome.

To get started with Joda-Time, try:

System.out.println( DateTime.now() );

Then search StackOverflow for "joda" or "joda date".

ISO 8601

When serializing date-time values to text storage, use the ISO 8601 format of YYYY-MM-DDTHH-MM-SS.sss+00:00 such as 2014-03-11T23:54:15+01:00 or 2014-03-11T22:54:15Z. This format is unambiguous. The format is intuitive across various cultures. The values when sorted alphabetically are also sorted chronologically.

The Joda-Time library uses the ISO 8601 format by default. Similarly the java.time package in Java 8 (inspired by Joda-Time, defined by JSR 310) also uses ISO 8601 but extends that format by appending in brackets the proper name of the time zone such as 2014-03-11T15:54:15+08:00[America/Los_Angeles].

store as long value returned from System.currentTimeMillis() and load back using Date date=new Date(long value);

You can use Calendar class or SimpleDateFormat as shown in below code:

import java.text.SimpleDateFormat;
import java.util.Calendar;

Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format.format(cal.getTime()));

Read more about Date formatting using SimpleDateFormat.

You'll need Date http://docs.oracle.com/javase/7/docs/api/java/util/Date.html And for formatting you need SimpleDateFormat

Date date = GregorianCalendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(date));

You can either use a text file or you could use the ObjectOutputStream because Date is Serializable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top