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.)

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

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)?

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

Hmm I used (before I read your replies):

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

Difference?

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.

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.

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

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

Perfect :)

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

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

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!

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.

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

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.

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?

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 :\

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.