Author Topic: [Java] Timestamps  (Read 4764 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[Java] Timestamps
« 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?

Code: [Select]
/**
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;
}

}
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Timestamps
« Reply #1 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));
    }

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] Timestamps
« Reply #2 on: August 19, 2006, 06:21:32 am »
Cool!
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [Java] Timestamps
« Reply #3 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Timestamps
« Reply #4 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.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Timestamps
« Reply #5 on: August 19, 2006, 04:26:56 pm »
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
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: [Java] Timestamps
« Reply #6 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.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [Java] Timestamps
« Reply #7 on: August 19, 2006, 04:56:07 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. ::)

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Timestamps
« Reply #8 on: August 19, 2006, 07:18:51 pm »
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. 

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Timestamps
« Reply #9 on: August 20, 2006, 04:50:17 am »
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
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.