0

I have a following String 07/21-04:00:14.766098 and I would like to format it to Date (or DateTime using Joda-Time or anything that I can easily compare, etc.). If I use SimpleDateFormat like this

SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd-HH:mm:ss.S" );
System.out.println(formatter.parse("07/21-04:00:14.766098"));

I get Tue Jul 21 04:13:00 CET 1970 which is not correct because it assumes that the numbers after seconds (766098) are miliseconds therefore it adds 13 minutes. So my question is, how to store that date correctly, so that I can compare dates that are thousandth apart?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Nirhu
  • 3
  • 2
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library – t0mppa Nov 20 '13 at 20:13
  • 1
    Regex might help, if you won't find any pre-cooked solution. Parsing this is easy. – MightyPork Nov 20 '13 at 20:19

2 Answers2

1

You can use SimpleDateFormat to parse your dates:

SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd-HH:mm:ss.S" );
Calendar c = Calendar.getInstance();
c.setTime( formatter.parse( "07/21-04:00:14.766098" ) );
c.set( Calendar.YEAR, 2012 );
System.out.println( new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss.S" ).format( c.getTime() ) );

The line

c.set( Calendar.YEAR, 2012 );

sets correct year because formatter.parse( "07/21-04:00:14.766098" ) returns 1970 year

UPDATE:

Class Timestamp allow you to add nanoseconds. To get your dates in Timestamp object you can split your date string into date & time part and fractionsl part. Parse date & time part into timestamp and then add nanoseconds.

there is example code:

    String dateString = "07/21-04:00:14.766098";

    String[] parts = dateString.split( "\\." );

    Calendar c = Calendar.getInstance();
    c.setTime( ( new SimpleDateFormat( "M/d-H:m:s" ) ).parse( parts[0] ) );
    c.set( Calendar.YEAR, 2014 );

    java.sql.Timestamp timestamp = new java.sql.Timestamp( c.getTimeInMillis() );

    if( parts.length == 2 ) {
        // Your fractional part must be 9 numbers length with tailing zeros
        // I am not sure how to pad string with zeros at right 
        int nanos = Integer.parseInt( String.format( "%-9s", parts[1] ).replace( ' ', '0' ) );
        timestamp.setNanos( nanos );
    }

    System.out.println( "Date string: " + dateString );
    System.out.println( "Timestamp  : " + timestamp.toString() );

The advantage of Timestamp is that it implements compassion and other useful methods.

Nicolai
  • 5,489
  • 1
  • 24
  • 31
  • Yes thats' much better. – Robert Moskal Nov 20 '13 at 20:34
  • Sorry, but what I get from this is `21/07/2012 04:13:00.98` which is not correct – Nirhu Nov 20 '13 at 23:09
  • This is different format, you need to use other format string - `dd/MM/yyyy HH:mm:ss.S`. If your sequence of dates has different formats, you can try to detect format using regular expressions or catching java.text.ParseException. If this not single run operation (e.g. you receive this sequence periodically) regular expressions will be better on my opinion. – Nicolai Nov 21 '13 at 08:31
  • No, `21/07/2012 04:13:00.98` is what I get when I run the code that you posted. As you can see it is 13 minutes later than what I had originallly. – Nirhu Nov 21 '13 at 08:39
  • Oh sorry. This is my mistake... Your date have microseconds but format "MM/dd-HH:mm:ss.S" tells that all numbers after dot are milliseconds. So there are 766098 milliseconds in the string. 766098 milliseconds is "13 minutes and 98 milliseconds". Unfortunately you can't parse microseconds using Java DateFormat. Only what I see is to parse date with regular expressions or remove last digits to remain only three digits after dot. – Nicolai Nov 21 '13 at 09:38
  • So what are you saying is that I can not have this precise date in Date object? Or any other object for that matter except my own? So to compare two dates like this I will create Date object in e.g. this format `dd/MM/yyyy HH:mm:ss` and store the rest in `String` because it can not be `int` (e.g. 007658 would be interpreted as 7658, which again would be incorrect) and when dates will be equal I will compare the rest somehow? That seems a bit complicated for something that shouldn't... – Nirhu Nov 21 '13 at 15:26
  • You can split date string into two strings 1 - date & time, 2 - microseconds. Then parse date & time part using SimpleDateFormat into Timestam, then convert microseconds string to long and add as `nanos` in Timestamp object. http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html – Nicolai Nov 21 '13 at 17:43
0

I don't know about date formats but it seems to me you have to parse the string. I'd split it on the '/' to get:

7 and 21-04:00:14.766098 Then I'd split the right side on the '-' to get: 21 and 04:00:14.766098.

That should give you what you need to construct a java Calendar or a JodaTime object. You may have to split up the remaining time component on ':' as well.

I'm curious as to whether there is an easier way.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86