Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calender Class

 
Jeremy Donaldson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hint:
http://developer.java.sun.com/developer/qow/archive/39/index.html
 
Jeremy Donaldson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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;
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic