News:

Pretty crazy that we're closer to 2030, than we are 2005. Where did the time go!

Main Menu

[Java] Timestamps

Started by Joe, August 19, 2006, 12:27:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Joe

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;
}

}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


iago

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));
    }

Joe

Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


rabbit

I do pretty much what iago did, except I don't use the fancy sprintf() like syntax.

iago

I should say that String.format() is new to 1.5, and one of my favorite new things.

MyndFyre

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
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Ender

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.

Sidoh

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

iago

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. 

MyndFyre

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
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.