Use an instance of SimpleDateFormat to format a new Date()
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
~s.o.s~
Failure as a human
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
DavidKroukamp
Practically a Master Poster
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
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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
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.
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
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
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