Valhalla Legends Forums Archive | Java Programming | Date() Class

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

[code]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
   }
}
[/code]
January 30, 2004, 4:08 PM
iago
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:
[quote] public 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) + "] ";
}
[/quote]

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>
January 30, 2004, 5:36 PM

Search