1

I have time stamp as 29-NOV-11 06.05.41.831000000 AM

Using SimpleDateFormat , i cannot really extract millisecond (.831000000) ....831 is millisecond (and i don't know why those 000000. But thats coming from different source. No control on that.)

Jigar Shah
  • 2,576
  • 6
  • 31
  • 53

4 Answers4

2

The extra digits are nano-second. You can't include these in SimpleDateFormat or the standard libraries. The simplest thing to do in you case is to remove them before parsing the date/time.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

java.time

Parse your date-time string into the OffsetDateTime using the timezone offset of UTC and convert the result into an Instant.

You can convert the Instant into milliseconds using Instant#toEpochMilli.

Demo:

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .appendPattern("d-MMM-uu h.m.s.n a")
                                .toFormatter(Locale.ENGLISH)
                                .withZone(ZoneOffset.UTC);
        
        OffsetDateTime odt = OffsetDateTime.parse("29-NOV-11 06.05.41.831000000 AM", dtf);
        Instant instant = odt.toInstant();
        long millis = instant.toEpochMilli();
        System.out.println(millis);
    }
}

Output:

1322546741831

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

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

If the timestamp is represented by a string you could use a regular expression to parse out the extra zeroes.

chooban
  • 9,018
  • 2
  • 20
  • 36
  • You have to be careful not to move too many digits. e.g. 41.831 is 41 seconds and 831 millis, but 41.83 is 42 seconds and 83 millis and 41.8 is parsed as 41 seconds and 8 millis. – Peter Lawrey Mar 12 '12 at 09:19
  • Yeh ..its string...Issue is those trailing zeros...how to use regular expression to convert them to mill + micro or just microsecond ? I need those milliseconds (and trailing AM/PM too) – Jigar Shah Mar 12 '12 at 09:35
  • 1
    @chooban It because it treats `/` or `-` or `.` as a separator. It doesn't recognise `.` as a special separator which means it is followed by a decimal. This is why 0.8310 won't work and it can become `8.310` – Peter Lawrey Mar 12 '12 at 12:08
  • That makes more sense. I can see myself putting together some more of those unit tests I have for core classes in order to understand them better. – chooban Mar 12 '12 at 13:37
0

A working solution... A fairly strict regex for this is quite long-winded as you see.

String input = "29-NOV-11 06.05.41.831000000 AM";

input = input
        .replaceFirst(
                "(\\d+-\\w+-\\d+\\s\\d+\\.\\d+\\.\\d+\\.\\d{3})\\d{6}(\\s(\\w+))",
                "$1$2");
try {
    SimpleDateFormat sdf = new SimpleDateFormat(
            "dd-MMM-yy HH.mm.ss.SSS aa");

    Date date = sdf.parse(input);
    System.out.println(sdf.format(date));

} catch (ParseException e) {
    e.printStackTrace();
}

You could just use substr if the strings are always fixed width and you consider regex a bit of overkill.

input = input.substring(0, 22) + input.substring(28, 31);
Adam
  • 35,919
  • 9
  • 100
  • 137