3

I'm having a little difficulty converting a string of the below format into a date. Keeps throwing a ParseException.

String: 2015-11-14 17:29:16.543934

I've looked around here already, notably this link: Java string to date conversion, but not having any luck.

Here's what my method currently looks like:

private Date parseTime(String input) {
    // input format: 2015-11-14 17:29:16.543934
    try {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssSSSSSS");
        Date date = format.parse(input);
        return date;
    } catch (ParseException e) {
        System.out.println("Could not parse time. ID: " + this.id);
    }
    return null;
}

EDIT:

On the answer I gave below (stating I missed a "." between the seconds and millis) Jon Skeet had a very good point: "That still won't work as you expect it to. It will treat your input as having 543934 milliseconds... so it will parse as being 17:38:19."

I actually don't need this level of precision so I'm just going to cut off the milliseconds, but if anyone knows how to keep the precision of 6 decimal points I would be interested to know how.

Community
  • 1
  • 1
JChristen
  • 588
  • 4
  • 21
  • You can keep the microsecond precision with either Java-8 (`java.time`-package) or with ThreetenBP (similar, but backport for Java-6+7) or with my library Time4J. Other libraries and the old JDK only offer millisecond precision. – Meno Hochschild Mar 23 '16 at 20:48

5 Answers5

2

You are missing the dot at the string format:

ss.SSSSSS

it must be:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Turns out this won't work for the reason Jon Skeet gave: "That still won't work as you expect it to. It will treat your input as having 543934 milliseconds... so it will parse as being 17:38:19." – JChristen Mar 23 '16 at 14:46
  • If SimpleDateFormat try to parse nanoseccond forcefully, I hear that operates like this. for example, 2015-05-08 00:00:00.123456→2015-05-08 00:02:03.000456 – nariuji Mar 23 '16 at 16:41
0

Turns out I was missing a "." between the seconds and milliseconds, so the new DateFormat is as follows:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
JChristen
  • 588
  • 4
  • 21
  • Note that milliseconds can only have up to 3 digits (milli = 1000, i.e. values from 0 to 999). – Thomas Mar 23 '16 at 14:16
  • 1
    That still won't work as you expect it to. It will treat your input as having 543934 milliseconds... so it will parse as being 17:38:19. – Jon Skeet Mar 23 '16 at 14:16
0

Declare this :

 String tt = "2015-11-14 17:29:16.543934";
    TextView textt;

Then used this :

             try{    
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
                    Date bod = format.parse(tt);
                    SimpleDateFormat rformat = new SimpleDateFormat("dd-MM-yyyy");
                    textt.setText(rformat.format(bod));
                }catch (ParseException pe){

                }

For Date Convert you have to used both Format.

  1. Format Before "yyyy-MM-dd HH:mm:ss.SSSSSS"
  2. Convert Format "dd-MM-yyyy"
0

Date can not use nanosecond, can it?

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS000");
nariuji
  • 288
  • 1
  • 6
0

'S' in SimpleDateFormat is milliseconds, and there nothing for microseconds.

In DateTimeFormatter (Java 8) 'S' means fractional part, and you can convert string without problems:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
LocalDateTime ldt = LocalDateTime.parse(input, dtf);
Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
Rustam
  • 1,397
  • 1
  • 13
  • 17