简体   繁体   中英

Date in String to Date in Date conversion

I'm having date in String format as "2019-10-30 12:17:47" . I want to convert this to an instance of Date along with the time so that I can compare two date obejcts.

This is what I've tried:

String dateString = "2019-10-30 12:17:47"        //Date in String format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH-mm-ss");    //sdf
Date d1 = format.parse(dateString);

But here I'm getting exception as "Unparseble date exception".

Kindly help...

What went wrong in your code?

In your format pattern string, yyyy-MM-dd HH-mm-ss , you have got two spaces between the date and the time. Since your date string, 2019-10-30 12:17:47 , has got only one space there, your formatter objects by throwing the exception. This was also what Tim Biegeleisen said in a comment. The comment by deHaar is true too: The hyphens between hour, minute and second don't match the colons in your date string either.

What to do instead?

See the good answer by deHaar

You should really switch to java.time (as already suggested in one of the comments below your question). It isn't more difficult than the outdated temporal classes from java.util but less error-prone and more powerful concerning offsets, time zones, daylight saving time and the multitude of different calendars the world has.

See this little example:

public static void main(String[] args) {
    String dateString = "2019-10-30 12:17:47";
    // define your pattern, should match the one of the String ;-)
    String datePattern = "yyyy-MM-dd HH:mm:ss";

    // parse the datetime using the pattern
    LocalDateTime ldt = LocalDateTime.parse(dateString,
                                            DateTimeFormatter.ofPattern(datePattern));

    // print it using a different (here a built-in) formatting pattern
    System.out.println(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    // or you just use the one defined by you
    System.out.println(ldt.format(DateTimeFormatter.ofPattern(datePattern)));
    // or you define another one for the output
    System.out.println(ldt.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH-mm-ss")));
}

The output on my system looks like this:

2019-10-30T12:17:47
2019-10-30 12:17:47
Okt 30 2019 12-17-47

The date in string you want to format does not match the formatter. See more detail here, https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

@Test
public void test2() {
    String dateString = "2019-10-30 12:17:47";        //Date in String format
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    //sdf
    try {
        Date d1 = format.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

There are two ways to do it

first is your way

    String dateString = "2019-10-30 12:17:47"; // Date in String format
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // sdf
    Date d1 = format.parse(dateString

second is my way (Local date)

    LocalDate resultDate = dateFormat("2019-10-30 12:17:47");
    System.out.println(resultDate);
  public static LocalDate dateFormat(String textTypeDateTime) {

    final DateTimeFormatter dateTimetextFormatter =
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return LocalDate.parse(textTypeDateTime, dateTimetextFormatter);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM