5

I have this code:

SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

I know that this code return hour, minute, second in the time. How i can get also the millisecond and microsecond??

lolo
  • 17,392
  • 9
  • 25
  • 49
  • It can be done, see my answer. – Peter Lawrey Jan 17 '12 at 14:27
  • 1
    Why is this -2??? I see no reason to downvote this question – MedicineMan Jan 25 '12 at 19:15
  • When I published it users was fighting on it, it was -3 and then +1... Sometimes users reduce points just because they high rated if you know what i mean... – lolo Jan 25 '12 at 19:26
  • 2
    I think it's more about your attitude. – Daniel T. Feb 14 '13 at 21:13
  • +1 because I also do not see any reason to downvote! – marc wellman Jan 02 '14 at 10:04
  • Are you after generating a string (formatting) with microseconds or after parsing a string with microseconds into a date-time object? – Ole V.V. Feb 13 '23 at 11:45
  • For new readers to this question I strongly recommend that you don’t use `SimpoleDateFormat`. It’s a notorious troublemaker of a class. Use classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Feb 13 '23 at 17:20

8 Answers8

7

You won't have microseconds, because a Date stores the number of milliseconds since Jan. 1 1970. For the milliseconds, use S, as documented in the javadoc.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
6

The only way to get micro-seconds is to parse the string yourself. Note: Date should be used to store micro-seconds, but you can use a long. (which you can also use for milli-seconds or nano-seconds)

private static final String YEARS_TO_MINUTES = "yyyy-MM-dd HH:mm";
private static final SimpleDateFormat YEARS_TO_MINUTES_SDF = new SimpleDateFormat(YEARS_TO_MINUTES);

public static long parseMicroSeconds(String text) throws ParseException {
    long timeMS;
    synchronized (YEARS_TO_MINUTES_SDF) {
        timeMS = YEARS_TO_MINUTES_SDF.parse(text.substring(0, YEARS_TO_MINUTES.length())).getTime();
    }
    long microSecs = 0;
    if (text.length() > YEARS_TO_MINUTES.length() + 1) {
        double secs = Double.parseDouble(text.substring(YEARS_TO_MINUTES.length() + 1));
        microSecs = (long) (secs * 1e6 + 0.5);
    }
    return timeMS * 1000 + microSecs;
}

public static String formatMicroSeconds(long timeMicroSeconds) {
    String dateTime;
    synchronized (YEARS_TO_MINUTES_SDF) {
        dateTime = YEARS_TO_MINUTES_SDF.format(new Date(timeMicroSeconds / 1000));
    }
    long secs = timeMicroSeconds % 60000000;
    return dateTime + String.format(":%09.6f", secs / 1e6);
}


public static void main(String... args) throws ParseException {
    String dateTime = "2011-01-17 19:27:59.999650";
    long timeUS = parseMicroSeconds(dateTime);
    for (int i = 0; i < 5; i++)
        System.out.println(formatMicroSeconds(timeUS += 175));
}

prints

2011-01-17 19:27:59.999825
2011-01-17 19:28:00.000000
2011-01-17 19:28:00.000175
2011-01-17 19:28:00.000350
2011-01-17 19:28:00.000525

You can do similarly if you need nano-timings.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi Peter, I understand this is off topic, but curious to know why YEARS_TO_MINUTES_SDF was synchronized. – Mahesh Feb 20 '17 at 09:51
  • @Mahesh SImpleDateFormat is not thread safe. It usually works fine without synchronization, but every so often you will get a corrupt value as it has mutable fields. – Peter Lawrey Feb 20 '17 at 09:52
3

The best you can do is:

SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
ante
  • 1,045
  • 10
  • 29
1
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class MyClass
{
    public static void main(String args[]) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss:SSSSS");
        for(int i=0; i<=5;i++) {
            System.out.println("Class 1 : " + ZonedDateTime.now().format(formatter));
        }
    }
}

Example output:

Class 1 : 19:25:19:77336
Class 1 : 19:25:19:77483
Class 1 : 19:25:19:77487
Class 1 : 19:25:19:77490
Class 1 : 19:25:19:77493
Class 1 : 19:25:19:77496
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

You can only show the millisecond: :SS.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

Check the Java-doc of SimpleDateFormat, it tells : Millisecond : S

Microseconds are not available.

Jerome L
  • 607
  • 5
  • 11
0

This example gets millisecond. Microseconds isn't available.

Date someDate = sDate.parse(dateString);
Calendar c = Calendar.setTime(someDate);
int millisecond = c.get(Calendar.MILLISECOND);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

Generally the milliseconds are given with capital s and in your case it will be yyyy-MM-dd HH:mm:ss:SSS. For more info refer to Customizing formats