This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calender Class" Watch "Calender Class" New topic

Calender Class

Jeremy Donaldson

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.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521

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

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);
case 1:

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

case 2:

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

case 3:

String time = timeFormat.format(cal.getTime());
System.out.println("current date and time: \t" + time);
Consider Paul's rocket mass heater.
subject: Calender Class
Similar Threads
Keeping gui and processing code seperate
could not find the main class
First Java Game - Help REALLY appreciated
JXAM: Vector vs. List