Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I've a (potentially) dumb question...
How does one retrieve the computers current date/time settings? (specifically date... though time would be useful)
Thanks y'all

Do a Google search on JAVA API and look at the Date class and the Gregorian Calendar class. The calendar functions are very robust and can do a lot things from comparing how many days/months/years/seconds/etc separate two dates to telling you whether a date is before today or not.
One thing to be aware of is that the Gregorian Calendar doesn't adjust the index of the months, so if you send month value 1, it returns Feb instead of Jan. Jan is value 0. You need to code to account for that. (The months are 0-11.) I showed two examples of formatting dates. . . there are many more. It can store a String for today to look like:
Wednesday, December 10, 2003
December 10, 2003
Dec 10, 2003
12/10/2003
etc.Use a getTime or getDate to get the current date. The following code shows how to make a new Gregorian Calender object and a new Date object.
GregorianCalendar dateTodayGreg = new GregorianCalendar();
Date dateToday = new Date();
DateFormat stdDateFormat = DateFormat.getDateInstance(DateFormat.FULL);
DateFormat stdDateFormat2 = DateFormat.getDateInstance(DateFormat.LONG);
String dateTodayString = stdDateFormat2.format(dateToday);
Good Luck.

I should probably be a little more specific... Heres a quick example.
import java.text.*;
import java.lang.*;
import java.util.*;public class DateTime{
public static void main(String args[]){
//Create a Date instance
Date myDate = new Date(System.currentTimeMillis());
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat tf = DateFormat.getTimeInstance(DateFormat.MEDIUM);
System.out.println("Time: "+tf.format(myDate)+" | Date: "+df.format(myDate));
}
}You should also download the java docs. They're very helpfull.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |