This isn't really worthy of even being posted, but oh well. Basically, it has a timestamp() function to get the current timestamp of the system clock. Anyone know an easy way of doing local time?
/**
This is a test class for my timestamp() method, described below
@author Joe[x86]
TODO: Time offset
*/
public class Timestamp
{
/** Damn Java's main() method for being static! */
public static void main(String args[]) { new Timestamp(); }
public Timestamp() { System.out.println(timestamp()); }
/**
Get's the current system time
*/
public String timestamp()
{
long mil = System.currentTimeMillis();
long sec = (mil / 1000) % 60;
long min = (mil / 60000) % 60;
long hr = (mil / 360000) % 24;
mil %= 1000;
return padLong(hr, 2) + ":" + padLong(min, 2) + ":" + padLong(sec, 2) + "." + padLong(mil, 3);
}
/**
Casts a long into a String and pads it to a specific length
@param l The long
@param length Length to pad it to
*/
private String padLong(long l, int length)
{
String ret = Long.toString(l);
for(int i = 0; i < length - ret.length(); i++)
{
ret = "0" + ret;
}
return ret;
}
}
This is untested, but ought to work:
Quote /** Returns the current time as a nicely formatted timestamp.
*/
public static String getTimestamp()
{
Calendar c = Calendar.getInstance();
return String.format("[%02d:%02d:%02d.%03d]",
c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
c.get(Calendar.SECOND),
c.get(Calendar.MILLISECOND));
}
Cool!
I do pretty much what iago did, except I don't use the fancy sprintf() like syntax.
I should say that String.format() is new to 1.5, and one of my favorite new things.
Quote from: iago on August 19, 2006, 09:43:04 AM
I should say that String.format() is new to 1.5, and one of my favorite new things.
Oh, they had to steal *another* thing from C#? :P
pfffft, you talk about java stealing things from c#! The above is true, but what about all the ways in which C# mimicked Java? xD.
Quote from: Ender on August 19, 2006, 04:30:26 PM
pfffft, you talk about java stealing things from c#! The above is true, but what about all the ways in which C# mimicked Java? xD.
Not to mention all of the ways it has transcended Java. ::)
Quote from: MyndFyrex86] link=topic=7101.msg88264#msg88264 date=1156019216]
Oh, they had to steal *another* thing from C#? :P
No, I believe it 'stole' the string formatting from C. Although it's possible it was used before that.
Quote from: iago on August 19, 2006, 07:18:51 PM
Quote from: MyndFyrex86] link=topic=7101.msg88264#msg88264 date=1156019216]
Oh, they had to steal *another* thing from C#? :P
No, I believe it 'stole' the string formatting from C. Although it's possible it was used before that.
Hehehehe. :) <3