Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: Joe on August 19, 2006, 12:27:11 AM

Title: [Java] Timestamps
Post by: Joe on August 19, 2006, 12:27:11 AM
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;
}

}
Title: Re: [Java] Timestamps
Post by: iago on August 19, 2006, 12:35:32 AM
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));
    }
Title: Re: [Java] Timestamps
Post by: Joe on August 19, 2006, 06:21:32 AM
Cool!
Title: Re: [Java] Timestamps
Post by: rabbit on August 19, 2006, 09:24:15 AM
I do pretty much what iago did, except I don't use the fancy sprintf() like syntax.
Title: Re: [Java] Timestamps
Post by: 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.
Title: Re: [Java] Timestamps
Post by: MyndFyre on August 19, 2006, 04:26:56 PM
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
Title: Re: [Java] Timestamps
Post by: 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.
Title: Re: [Java] Timestamps
Post by: Sidoh on August 19, 2006, 04:56:07 PM
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. ::)
Title: Re: [Java] Timestamps
Post by: 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. 
Title: Re: [Java] Timestamps
Post by: MyndFyre on August 20, 2006, 04:50:17 AM
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