1

I'm trying to convert a string to date

I tried in couple ways but i'm getting the same.Please could any one help me out.

String stringDate ="20181228184821";
Date date = null;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
    date = sdf1.parse(stringDate);
    System.out.println(date);
} catch (ParseException e) {
    System.out.println("ICICIBusinessLogic Error in converting input date string ****" + e);
}

I get the following exception :

Error in converting input date string ****java.text.ParseException: Unparseable date: "20181228184821"

6
  • 2
    Your format requires a space and two colons. The value you're trying to parse has none of those. Commented Dec 31, 2018 at 5:25
  • Date you are providing is unacceptable format "20181228184821" like 2018-12-28 18:48:21 Commented Dec 31, 2018 at 5:28
  • Thanks for the response but i'm getting the date String from the service with above mentioned format. Commented Dec 31, 2018 at 5:31
  • use SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss") Commented Dec 31, 2018 at 5:37
  • 1
    I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Commented Dec 31, 2018 at 7:54

4 Answers 4

2

Your stringDate should exactly match your format.

20181228184821 => 20181228 18:48:21 should work.

Or change your format to yyyyMMddHHmmss

Sign up to request clarification or add additional context in comments.

Comments

1

You should use DateTimeFormatter and the new classes in java.time instead of the old SimpleDateFormat api. That being said, you appear to have confused your input and your output formats.

String fmtIn = "yyyyMMddHHmmss", fmtOut = "yyyyMMdd HH:mm:ss";
String stringDate = "20181228184821";
TemporalAccessor dt = DateTimeFormatter.ofPattern(fmtIn).parse(stringDate);
System.out.println(LocalDateTime.from(dt));
System.out.println(DateTimeFormatter.ofPattern(fmtOut).format(dt));

3 Comments

Is there any considerable issues of using SimpleDateFormat compared to DateTimeFormatter?
@AtheeshRathnaweera There are too many to mention here. SimpleDateFormat is causing a lot of trouble for many programmers, and its fellows like Date are poorly designed too.
@ElliottFrisch Going through a TemporalAccessor is not the recommended way. Just do LocalDateTime dt = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern(fmtIn)); (and drop the from call in the following line). Thanks for showing the use of the modern API.
1

You are using the wrong pattern, use the following instead :

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");

Also, SimpleDateFormat is long outdated, make use of java-8's java.time

LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

Comments

0

The pattern specified in Simple Date Format Constructor should be same as the string to be parsed.

Try String stringDate ="20181228 18:48:21";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.