954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to get date and time with miliseconds?

Hey

I want to get the date and time with miliseconds in a format such as:

2012-02-09 12:41:52.982

or

2012-02-09 16:41:52.129

How can I do this in Java?

(If I can get a localized version of "Tuesday" as "Tues" as well even better but if not, I dont mind. Something stupid and unrequired.)

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

Use an instance of SimpleDateFormat to format a new Date()

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Use an instance of SimpleDateFormat to format a new Date()

Yes...

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");

But how do I include the miliseconds (and the localized version of the day of the week)?

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

The format `dd/MM/yyyy - HH:mm:ss.S E` should do the trick. For future references, look into the Javadoc of the SimpleDateFormat class .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Yes...

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");

But how do I include the miliseconds (and the localized version of the day of the week)?


Check this

Dim MyTime As String
MyTime = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss.fff")


It gives the output:
09-Feb-2012 18:08:14.403

Sorry its in vb.net

poojavb
Posting Whiz
326 posts since Nov 2011
Reputation Points: 31
Solved Threads: 37
 

Hmm I used (before I read your replies):

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss.SSS",Locale.getDefault());


Difference?

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

The difference is that with SSS, you are providing a padding for the millisecond field. So if the milliseconds elapsed are 9, it'll be shown as "009". If you instead had just "S", it would be shown as "9". Regarding the 'E' format specifier, you can simply replace the 3 E's with a single E without any visible change in the output. If you have more than or equal to 4 E's, it'll use the full form of the day of the week. For e.g. with a single E, the output would be 'Fri' but with 4 E's it would be 'Friday'.

Also, the locale for a date formatter is by default the default locale so you can drop the second argument to the SimpleDateFormat constructor.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

The difference is that with SSS, you are providing a padding for the millisecond field. So if the milliseconds elapsed are 9, it'll be shown as "009". If you instead had just "S", it would be shown as "9". Regarding the 'E' format specifier, you can simply replace the 3 E's with a single E without any visible change in the output. If you have more than or equal to 4 E's, it'll use the full form of the day of the week. For e.g. with a single E, the output would be 'Fri' but with 4 E's it would be 'Friday'.

Also, the locale for a date formatter is by default the default locale so you can drop the second argument to the SimpleDateFormat constructor.


Great, I perfer the 3 digit milisecond count.

Is the locale the default system locale or the default locale from the machine the code was complied from? Besides, doesnt hurt to leave it.

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

Great, I perfer the 3 digit milisecond count.

Is the locale the default system locale or the default locale from the machine the code was complied from? Besides, doesnt hurt to leave it.


Isnt the system locale from the one the user put in when they first installed windows i.e United States etc....but you can create your 'own': http://java.sun.com/developer/technicalArticles/J2SE/locale/ and see here: http://www.exampledepot.com/egs/java.util/SetDefLocale.html

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

In theory it uses the locale of the machine where it is running - but in practice that's often US for some reason.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Perfect :)

If it outputs in the complied language, oh well.....

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

I created this:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateClass
{
	
	public String GetTimeNow()
	{	
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss.SSS",Locale.getDefault());
		Calendar cal = Calendar.getInstance();
		return dateFormat.format(cal.getTime()).toUpperCase();
	}

}


But when I try:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class SomeOtherClass
{
	
	public void SomeOtherFunction()
	{	
            DateClass d=new DateClass();
            System.out.println("Time now 1: " + d.GetTimeNow());
            System.out.println("Time now 2: " + d.GetTimeNow());
            System.out.println("Time now 3: " + d.GetTimeNow());
	}

}


All three lines are the same when there should be at least a milisecond of difference right? What Im wondering if it gets the same time or if it is different (each calling) and it really is that quick to run the code.

Thank you

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

Calendar.getInstance(); really does get the latest time each time you execute it. but...

You have a machine that clocks at - I don't know but maybe 1GHz give or take a factor of 3? That's 1 million clock cycles per milliSec. That's can be a huge amount of calculation.
Also most ordinary desktop computers get the time from a simple traditional clock circuit that updates every 1/60 sec. So if you keep printing the time in mSec in a loop you will probably see it jumping in increments of 16 mSec. Not a lot you can do about that, other than go out and buy a proper workstation!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

You can get the time in milliseconds very easily... It's System.getTime... something. If you're using an IDE that gives suggestions, just write "System." and that func will come up. :) Hope this helps.

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 21
 
You can get the time in milliseconds very easily... It's System.getTime... something. If you're using an IDE that gives suggestions, just write "System." and that func will come up. :) Hope this helps.

That's not his problem. He already has code that gets the time in mSec and in a class where he can format it. Maybe you should read threads more carefully before posting

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

also: depending on your IDE to 'auto-complete' your code is a crappy way of writing applications. better taking a few seconds longer, but knowing and understanding what you wrote when compared to knowing 'NetBeans(/Eclipse/...)' 'll solve the problem for me.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Hey

I want to get the date and time with miliseconds in a format such as:

2012-02-09 12:41:52.982

or

2012-02-09 16:41:52.129

How can I do this in Java?

(If I can get a localized version of "Tuesday" as "Tues" as well even better but if not, I dont mind. Something stupid and unrequired.)

This?

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 21
 
also: depending on your IDE to 'auto-complete' your code is a crappy way of writing applications. better taking a few seconds longer, but knowing and understanding what you wrote when compared to knowing 'NetBeans(/Eclipse/...)' 'll solve the problem for me.

I only said that because I didn't remember the function name exactly :\

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 21
 

No ... THIS

don't just read the first post.

I created this:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateClass
{
	
	public String GetTimeNow()
	{	
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss.SSS",Locale.getDefault());
		Calendar cal = Calendar.getInstance();
		return dateFormat.format(cal.getTime()).toUpperCase();
	}

}

But when I try:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class SomeOtherClass
{
	
	public void SomeOtherFunction()
	{	
            DateClass d=new DateClass();
            System.out.println("Time now 1: " + d.GetTimeNow());
            System.out.println("Time now 2: " + d.GetTimeNow());
            System.out.println("Time now 3: " + d.GetTimeNow());
	}

}

All three lines are the same when there should be at least a milisecond of difference right? What Im wondering if it gets the same time or if it is different (each calling) and it really is that quick to run the code.

Thank you

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Calendar.getInstance(); really does get the latest time each time you execute it. but...

You have a machine that clocks at - I don't know but maybe 1GHz give or take a factor of 3? That's 1 million clock cycles per milliSec. That's can be a huge amount of calculation. Also most ordinary desktop computers get the time from a simple traditional clock circuit that updates every 1/60 sec. So if you keep printing the time in mSec in a loop you will probably see it jumping in increments of 16 mSec. Not a lot you can do about that, other than go out and buy a proper workstation!

OK....then I guess it is just that. It cant get it EXACTLY EXACTLY because as you said, my PC is too "fast" lol....

Thanks

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You