aspose file tools*
The moose likes Beginning Java and the fly likes Calender Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calender Class" Watch "Calender Class" New topic
Author

Calender Class

Jeremy Donaldson
Greenhorn

Joined: Jun 21, 2001
Posts: 11
I'm working through the exercises from the cattle barn and I'm having trouble understanding how to complete the geek watch. Before they were deprecated I could use the date class to pull the exact information that exercise 8 requests. But using the Calendar class I'm not quite sure how to find the days, hours, minutes and seconds since January 1, 1970. If anyone could throw me a hint that would be great.
Thanks!
Jeremy
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
hint:
http://developer.java.sun.com/developer/qow/archive/39/index.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jeremy Donaldson
Greenhorn

Joined: Jun 21, 2001
Posts: 11

Thanks for the hint. Here was my attempt. The requirements are expressed within the switch statement. I'm not quite sure how to capture the current date. Currently I'm posting just the time. Any additional help would be great.
import java.util.*;
import java.text.*;
class GeekWatch {

public static void main (String args[]) {

int x = Integer.parseInt(args[0]);
DateFormat timeFormat = DateFormat.getTimeInstance();
Date tf = new Date();
Calendar cal = Calendar.getInstance();
long mill = tf.getTime();

switch (x) {

case 0:

System.out.println("milliseconds since January 1, 1970: \t" + mill);
break;
case 1:

long seconds =mill/1000;
System.out.println("number of seconds since January 1, 1970: \t" + seconds);
break;

case 2:

long day = ((((mill/1000)/60)/60)/24);
System.out.println("number of days since January 1, 1970: \t" + day);
break;

case 3:

String time = timeFormat.format(cal.getTime());
System.out.println("current date and time: \t" + time);
break;
}
}
}
 
Don't get me started about those stupid light bulbs.
 
subject: Calender Class