• Welcome to Valhalla Legends Archive.
 

Date() Class

Started by effect, January 30, 2004, 10:08 AM

Previous topic - Next topic

effect

A simple Date class i did i believe its pretty newby friendly , maybe someone can use it for something.

import java.util.Date;

public class GetDate {

   public static void main(String[] args) {
   
   Date newDate;
      
      newDate = new Date(); //initialize the new class and allocate memory for it
      
      System.out.println(newDate); //unformated Date
      
            //retrieve the current day , Java stores the day as a single byte?
         if (newDate.getDay() == 1)
            System.out.println("Day = Monday");
         if (newDate.getDay() == 2)
            System.out.println("Day = Tuesday");
         if (newDate.getDay() == 3)
            System.out.println("Day = Wednesday");
         if (newDate.getDay() == 4)
            System.out.println("Day = Thursday");
         if (newDate.getDay() == 5)
            System.out.println("Day = Friday");
         if (newDate.getDay() == 6)
            System.out.println("Day = Saturday");
         if (newDate.getDay() == 7)
            System.out.println("Day = Sunday");
            //parsing the rest of the Methods (getHour , GetMonth etc) the same way or how ever u like
   }
}
Quote from: Mangix on March 22, 2005, 03:03 AM
i am an expert Stealthbot VBScript. Recognize Bitch.

iago

#1
There's already a date class ("Date") and a Calendar class ("Calendar") which are pretty simple to use :)

But, on this topic, I wrote a timestamp class:
Quotepublic String padNumber(int num, int size) throws Exception
   {
       String s = "" + num;

       while(s.length() < size)
           s = "0" + s;

       return s;
   }

   public String getTimeStamp() throws Exception
   {
       // Get the timestamp
       Calendar date = Calendar.getInstance();
       return "[" + padNumber(date.get(Calendar.HOUR_OF_DAY), 2) + ":" + padNum
ber(date.get(Calendar.MINUTE), 2) + ":" + padNumber(date.get(Calendar.SECOND), 2
)+ "." + padNumber(date.get(Calendar.MILLISECOND), 3) + "] ";
   }

Excuse the lousy error handling, though, I just threw the whole program together without wanting to worry about it.  



<edit>Incidentally, Date.getDay() and the others you mentioned are deprecated and should be replaced with equivolant functions from class Calendar</edit>
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*