1

Is there any way in java(java.util.* or Joda api ) to convert "2020-04-03 20:17:46" to "yyyy-MM-dd'T'HH:mm:ss"

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
          .parse("2020-04-03 20:17:46")

its giving java.text.parseException always

user3190018
  • 890
  • 13
  • 26
  • Two things you can do. Either parse the date using the format that it's in, then format it using the new format; or just text replace the space with a T. – Dawood ibn Kareem Apr 08 '20 at 19:46
  • 4
    Can't you use `java.time`? – deHaar Apr 08 '20 at 19:47
  • 1
    I recommend you neither use `SimpleDateFormat` nor `java.util.*` for this. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 11 '20 at 14:40

6 Answers6

4

Just for the case you are using Java 8 or above, make use of java.time.
See this simple example:

public static void main(String[] args) {
    // example datetime
    String datetime = "2020-04-03 20:17:46";
    // create a formatter that parses datetimes of this pattern
    DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    // then parse the datetime with that formatter
    LocalDateTime ldt = LocalDateTime.parse(datetime, parserDtf);
    // in order to output the parsed datetime, use the default formatter (implicitly)
    System.out.println(ldt);
    // or format it in a totally different way
    System.out.println(ldt.format(
            DateTimeFormatter.ofPattern("EEE, dd. 'of' MMM 'at' hh-mm-ss a",
                    Locale.ENGLISH)
            )
    );
}

This outputs

2020-04-03T20:17:46
Fri, 03. of Apr at 08-17-46 PM

Please note that this doesn't consider any time zone or offset, it just represents a date and time consisting of the passed or parsed years, months, days, hours, minutes and seconds, nothing else.

deHaar
  • 17,687
  • 10
  • 38
  • 51
3

Do not use Date/Time API from java.util.* as most of them are now outdated. Use java.time API instead.

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) throws IOException {
        String strDatetime = "2020-04-03 20:17:46";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
        System.out.println(parsedDate);
    }
}

Output:

2020-04-03T20:17:46

Learn more about DateTimeFormatter at https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Could this help you? http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

First you need to parse the String with the old format, you will get a Date object. Then Create a new SimpleDateFormat with your new format, then you can format the Date object.

CodingTil
  • 447
  • 2
  • 16
1
    String dateString = "2020-04-03 20:17:46";
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sdf.parse(dateString);
    String formattedDate = output.format(date);
0

It do not work that way directly but if you still want to do it then, here is the process.

Create an object of SimpleDateFormat with pattern "yyyy-MM-dd HH:mm:ss")

use this to parse the string. Ultimately you are going to get date in both cases. Is there any specific reason for using T in pattern for dates which do not contain them?

0

Use LocalDateTime.

    Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");

    LocalDateTime localDateTime = timestamp.toLocalDateTime();  

    System.out.println(localDateTime); // 2020-04-03T20:17:46