• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calender Class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic