0

I have an input string of the format dd/MM/yyyy, I need to convert it into date dd/MM/yyyy.

My approach is:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(formatter.parse("22/09/2016"));
Date convertedDate = formatter.parse(date);

I was expecting 22/09/2016 as a date object, however the format returned was not as expected. O/P=>Mon Sep 12 00:00:00 IST 2016

Any idea where I am going wrong? Thanks in advance!

mnille
  • 1,328
  • 4
  • 16
  • 20
robot_alien
  • 955
  • 2
  • 13
  • 30
  • I think you are overcomplicating things. Check out [this answer](http://stackoverflow.com/a/6252782/5292801) – Takarii Sep 22 '16 at 13:06
  • 1
    You should better describe what's wrong, from your point of view. Because `formatter.parse("22/09/2016")` does already what you want `I was expecting 22/09/2016 as a date object`. – SubOptimal Sep 22 '16 at 13:11
  • @Takarii I'm not using joda library – robot_alien Sep 22 '16 at 13:51

4 Answers4

6

You seem to be assuming that a java.util.Date "knows" a format. It doesn't. That's not part of its state - it's just the number of milliseconds since the Unix epoch. (There's no time zone either - the IST you're seeing there is your local time zone; that's just part of what Date.toString() does.)

Basically, a Date is just an instant in time - when you want a particular formatted value, that's when you use SimpleDateFormat.

(Or better, use java.time.*...)

Think of it like a number - the number sixteen is the same number whether you represent it in binary as 10000, decimal as 16, or hex as 0x10. An int value doesn't have any concept of "I'm a binary integer" or "I'm a hex integer" - it's only when you convert it to a string that you need to care about formatting. The exact same thing is true for date/time types.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

tl;dr

LocalDate.parse( "22/09/2016" , DateTimeFormatter.ofPattern( "dd/MM/yyyy" ) )
         .format( DateTimeFormatter.ofPattern( "dd/MM/yyyy" ) )

Problems

  • You are conflating date-time objects with strings representing date-time values. A date-time object can parse a string, and can generate a string, but the date-time object is always separate and distinct from the string. The string has a format; the date-time object does not.
  • You are using troublesome old date-time classes now supplanted by the java.time classes.
  • You are trying to fit a date-only value into a date-time object (square peg, round hole).
  • You are being tricked by the poorly-designed toString method that silently applies a time zone to an internal value that has no time zone (is UTC).

Be sure to read the correct Answer by Jon Skeet.

java.time

Using the new java.time classes, specifically LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/yyyy" );
LocalDate ld = LocalDate.parse( "22/09/2016" , f );

Generate a String to represent that value in standard ISO 8601 format by calling toString.

String output = ld.toString(); // 2016-09-22

Generate a String in your desired format by applying the formatter.

String output = ld.format( f );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old 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.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
 try {
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            Date d = formatter.parse("22/09/2016");
            System.out.println(d.toString());
            String e = formatter.format(d);
            System.out.println(e);
        } catch (ParseException ex) {
            Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex);
        }
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
1

A Date, when printed will call the toString method of the object. It will then choose whatever format it wants.

Try

System.out.println(formatter.format(convertedDate));

or - obviously

System.out.println(date);
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213