StringDate to Date coming in different Time in SimpleDateFormat in java

懵懂的女人 提交于 2021-01-29 08:52:00

问题


    /*I want the same Date of my String has
     Tried with couple of options but nothing worked 
     pasting some of code here */

    public void stringToDate() {

        //Current format "13-FEB-20 03.21.08.100000000 PM" in Melbourne Timezone
        //Required Format yyyy-dd-MM HH:mm:ss.SSS in Melbourne Timezone

        String inputAM = "13-FEB-20 03.21.08.100000000 PM";
        try {
            DateFormat df1 = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
            Date d1 = df1.parse(inputAM);
            System.out.println("Date-1: " + d1); //Fri Feb 14 19:07:48 AEDT 2020

            DateFormat df2 = new SimpleDateFormat("DD-MMM-yy hh.mm.ss.S aa");
            Date d2 = df2.parse(inputAM);
            System.out.println("Date-2: " + d2); //Tue Jan 14 19:07:48 AEDT 2020

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat etDf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss.SSS");
        TimeZone etTimeZone = TimeZone.getTimeZone("Australia/Melbourne");
        etDf.setTimeZone(etTimeZone);

        DateFormat df3 = new SimpleDateFormat("dd-MMM-yy HH.mm.ss.SSSSSSSSS a");
        Date d3;
        try {
            d3 = df3.parse(inputAM);
            System.out.println("Date-3: " + d3); //Fri Feb 14 07:07:48 AEDT 2020
            System.out.println("Date-4: " + etDf.format(d3.getTime())); //2020-14-02 07:07:48.000
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

回答1:


First, for most purposes don’t convert a date and time from one string format to another. In your program keep date and time as proper date-time objects, not strings. When you accept string input, parse it first thing. Only when you need to give string output, format your date-time into a string in the required format.

Second, use java.time, the modern Java date and time API for all of your date and time work. It is so much nicer to work with than the old, poorly designed and long outdated classes including DateFormat, SimpleDateFormat and Date.

Parse your input using java.time

    //Current format "13-FEB-20 03.21.08.100000000 PM" in Melbourne Timezone
    DateTimeFormatter currentFormatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("d-MMM-uu hh.mm.ss.SSSSSSSSS a")
            .toFormatter(Locale.ENGLISH);
    ZoneId zone = ZoneId.of("Australia/Melbourne");

    String inputAM = "13-FEB-20 03.21.08.100000000 PM";

    ZonedDateTime dateTime = LocalDateTime.parse(inputAM, currentFormatter).atZone(zone);

    System.out.println(dateTime);

Output so far is:

2020-02-13T15:21:08.100+11:00[Australia/Melbourne]

Format using java.time

    //Required Format yyyy-dd-MM HH:mm:ss.SSS in Melbourne Timezone
    DateTimeFormatter requiredFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    String formatted = dateTime.format(requiredFormatter);
    System.out.println(formatted);

2020-02-13 15:21:08.100

What went wrong in your code?

When I first tried your code, parsing failed for FEB because your code doesn’t specify locale, and my Java uses a non-English default locale.

Correcting that I was able to parse the string and got the same results as you (only in my time zone). What happens: SimpleDateFormat takes uppercase S to mean milliseconds, 1000ths of seconds, no matter how many S there are and no matter how many digits are in the string to be parsed. So 100 000 000 milliseconds were added to your date and time. That’s a little more than one day. So you got 14 Feb instead of 13 Feb, and also a wrong time of day. In contrast to the modern DateTimeFormatter upper case S means fraction of second, so it handles both SSSSSSSSS and SSS the way we expect.

Uppercase DD is for day of year, so using this for parsing you got the 13th day of the year (the same as 13 January) plus your 100 000 seconds.

Uppercase HH is for hour of day from 00 through 23. Parsing using HH gave you 03:21 of day (same as 03:21 AM) plus your 100 000 seconds no matter that it said PM in your string.

Link

  • Oracle tutorial: Date Time explaining how to use java.time.


来源:https://stackoverflow.com/questions/60408294/stringdate-to-date-coming-in-different-time-in-simpledateformat-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!