converting date to string and back to date in java

2020-02-15 02:56发布

问题:

In my program I am trying to convert the date to string to specified format and then back to date. I need the date to be in dd-MMM-yy format. So, I am converting the date to dd-MMM-yy format using SimpleDateFormat like,

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
String bookingDate = sdf.format(myDate);

The result is what I expected, that is 23-May-12 but when I convert the string back to date using parse(), it is changing to Wed May 23 13:16:14 IST 2012.

Is there any way to convert the string back to date without changing format? I strictly need to pass the date object in the specified format to the query.

回答1:

when i convert the string back to date using parse(), it is changing to Wed May 23 13:16:14 IST 2012.

That is not true, It converts back to Date correctly, When you try to print Date instance, It invokes toString() method and it has fixed formated output so if you want the formatted date you need to use format() method

In short parse method parses the String to Date there is no property of Date which holds format so you need to use format() method anyhow



回答2:

How are you passing the date object in your query, if you are simply appending date in your query string why you need to convert your string date back to date object ? And If you are using prepared statement then you need not worry about the format. And as suggested above Date object does not hold any format information



回答3:

If You strictly need to pass the date object then there is no worries about its format as Date object has understanding with its format either Java, any java framework or SQL..

And if you are passing the date directly in sql query (concatenating) then you can pass Date String in the same format you see in DB, it will process the same correctly.



回答4:

You can use DateFormat.getDateInstance(DateFormat.SHORT, <locale>).parse(myDate);.

You will, however, have a date in a standard format like dd/MM/yy, not custom. Template depends on which Locale you use.

You can try building your own Pattern using the standard Java Library or through ThreeTen/JSR-310



回答5:

We now have a more modern way to do this work.

java.time

The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome confusing old classes, java.util.Date/.Calendar et al.

Date-only class

The new framework offers a class, LocalDate, to represent a date-only without any time-of-day nor time zone.

By the way, using two-digits for year is asking for trouble. I strongly recommend using four-digit years whenever possible. The java.time framework assumes any two-digit years are in the 20xx century, years 2000 to 2099 inclusive.

To parse the string, specify the expected format. You are not using any of the standard ISO 8601 formats, so we must define a format pattern. Specify a Locale that includes the language we expect to find for name-of-month.

// Parse String representation.
String input = "23-May-12";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "d-MMMM-yy" , Locale.ENGLISH );  // Using four "M" characters means we expect the full name of the month.
LocalDate localDate = formatter.parse ( input , LocalDate :: from );

To go the other direction, to generate a string representation of the date value, we can recycle the same formatter.

    // Generate String representation.
    String output = localDate.format ( formatter );

Dump to console.

    System.out.println ( "input: " + input + " → localDate : " + localDate );
    System.out.println ( "localDate : " + localDate + " → output: " + output );

When run.

input: 23-May-12 → localDate : 2012-05-23

localDate : 2012-05-23 → output: 23-May-12