rossjAva2015
rossjAva2015

Reputation: 23

Setting DateFormat to current date and time

I am using java.text.DateFormat in order to display the date and time for a user of my application. Below is my code to test the output.

The issue is that the date is being displayed as 1970 (see output below). How can I update this to the current date and time.

Current Output:

 1 Jan 1970 01:00:00

Current code:

DateFormat[] formats = new DateFormat[] {

        DateFormat.getDateTimeInstance(),

        };

        for (DateFormat df : formats) {

            Log.d("Dateformat", "Date format: " + (df.format(new Date(0))));
        }

Alternatively if the above is not possible, I am able to get the current time and date using the following method:

Time now = new Time();
        now.setToNow();
        String date= now.toString();

Output:

20140722T133458Europe/London(2,202,3600,1,1406032498)

How can I adjust this in order to make it readable for a user?

Upvotes: 0

Views: 118

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339382

tl;dr

Instant.now()
       .atZone( ZoneId.of( "America/Montreal" ) )
       .format( DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
                                 .withLocale( Locale.CANADA_FRENCH ) 
       )

Instant

The accepted Answer by Wallace is correct.

But know that you are using troublesome old date-time classes now supplanted by the java.time classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now();  // Current moment in UTC.

To generate a String representing that moment formatting according to the ISO 8601 standard, simply call toString.

ZonedDateTime

To view the same moment through the lens of some region’s wall-clock time, apply a ZoneId to get a ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" ); 
ZonedDateTime zdt = instant.atZone( z );  // Adjust from UTC to a specific time zone. Same moment, different wall-clock time.

DateTimeFormatter

For presentation to the user, let java.time automatically localize using the DateTimeFormatter class.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such.

Example:

Locale l = Locale.CANADA_FRENCH ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
String output = zdt.format( f );

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, & java.text.SimpleDateFormat.

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

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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

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.

Upvotes: 0

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

Use this -

String S = new SimpleDateFormat("MM/dd/yyyy").format(System.currentTimeMillis());

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79847

Just write new Date() instead of new Date(0) in your first snippet. When you write new Date(some number) it makes a date which is that many milliseconds after 1/1/1970 00:00:00Z

Upvotes: 3

Related Questions