2

I have formatted date in the form of string and i want it in date format without changing formatted pattern

here is my code

Date currDate = new Date();//Fri Oct 31 03:48:24 PDT 2014

    String pattern = "yyyy-MM-dd hh:mm:ss";
    SimpleDateFormat formatter;
    formatter = new SimpleDateFormat(pattern);
    String formattedDate= formatter.format(currDate);//2014-10-31 04:23:42

here am getting in "yyyy-MM-dd hh:mm:ss" format and the same format i want it in date.

 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 Date paidDate = sdf.parse(formattedDate);

System.out.println(pattern + " " + paidDate);//Fri Oct 31 03:48:24 PDT 2014

but i am getting result as Fri Oct 31 03:48:24 PDT 2014, so pls help me to get result as 2014-10-31 04:23:42 in date format

user2894607
  • 165
  • 4
  • 14
  • 2
    Why don't you use `formatter.format(currDate)` in your `System.out.println` statement? – Stefan Freitag Oct 31 '14 at 11:02
  • Change your pc default date format what you want and then `System.out.println(new Date())`http://docs.oracle.com/cd/E12547_01/books/Sales_AppsConfigV1.2/Sales_AppsConfig_SalesProspector11.html – Ruchira Gayan Ranaweera Oct 31 '14 at 11:08
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 10 '18 at 06:17

4 Answers4

3

If I understood your problem correctly:

System.out.println(pattern + " " + sdf.format(paidDate);
3

You seem to be under the mistaken impression that a Date object somehow encodes format of the original date. It doesn't.

So ... when you do this:

Date paidDate = sdf.parse(formattedDate);

it does not "remember" original format of the text form of the date in paidDate. And it cannot. If you want to print / unparse a Date in any format than the default one, you should use a DateFormat and call its format method. Calling toString() will just give you the date in the default format.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Try this.

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

0

tl;dr

Do not conflate a date-time object with a string representing its value. A date-time object has no “format”.

ZonedDateTime.now(                                         // Instantiate a `ZonedDateTime` object capturing the current moment.
    ZoneId.of( "Africa/Tunis" )                            // Assign this time zone through which we see the wall-clock time used by the people of this particular region.
).format(                                                  // Generate a string representing the value of this `ZonedDateTime` object.
    DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" )   // Define a formatting pattern to match your desire.
)

2018-03-10 07:36:23

Calling ZonedDateTime::toString generates a string in standard ISO 8601 format.

2018-03-10T07:36:23.595362+01:00[Africa/Tunis]

Date-time object has no “format”

You are confusing a date-time object in Java, or a date-time value stored in a database, with a textual representation. You can generate a string from a date-time object (or database value), but that string is separate and distinct from the value it represents. Do not conflate a string with its generating creator.

java.time

Avoid using the troublesome old date-time classes such as Date, Calendar, and SimpleDateFormat. Instead, use Instant, ZonedDateTime, and DateTimeFormatter classes, respectively.

If you have an input string such as 2014-10-31 04:23:42, replace the SPACE in the middle with a T to comply with ISO 8601 standard format. The java.time classes use ISO 8601 formats by default when parsing/generating strings.

String input = "2014-10-31 04:23:42".replace( " " , "T" ) ;

That input lacks any indicator of time zone or offset-from-UTC. So parse as a LocalDateTime which purposely lacks any concept of zone/offset.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

ldt.toString(): 2014-10-31T04:23:42

A LocalDateTime does not represent a moment, is not a point on the timeline. To determine an actual moment, you must supply the context of a zone or offset.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;  // Now we have an actual moment, a point on the timeline.

To capture the current moment in UTC, use Instant.

Instant instant = Instant.now() ;

Adjust into another zone.

ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString(): 2018-03-10T07:36:23.595362+01:00[Africa/Tunis]

I do not recommend generating strings lacking an indicator of zone/offset. But if you insist, use the built-in DateTimeFormatter and then replace the T in the middle with a SPACE to get your desired format.

String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " ) ;

2018-03-10 07:36:23.595362

If you really do not want the fractional second, then define your own formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" ) ; 
String output = zdt.format( f ) ;

2018-03-10 07:36:23


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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.

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