Computing.Net > Forums > Programming > Java- Autofinding date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java- Autofinding date

Reply to Message Icon

Name: Matty
Date: December 10, 2003 at 17:02:26 Pacific
OS: WinXP
CPU/Ram: slow and not enough
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: fickman
Date: December 10, 2003 at 18:08:10 Pacific
Reply:

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.


0

Response Number 2
Name: gpp
Date: December 11, 2003 at 05:50:36 Pacific
Reply:

java.util.Date myDate = new java.util.Date(System.currentTimeMillis());


0

Response Number 3
Name: gpp
Date: December 11, 2003 at 06:14:45 Pacific
Reply:

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.


0

Response Number 4
Name: matty
Date: December 11, 2003 at 07:09:10 Pacific
Reply:

Thanks y'all


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java- Autofinding date

cannot Sort String Array in JAVA www.computing.net/answers/programming/cannot-sort-string-array-in-java-/8805.html

Sending email automatically by java www.computing.net/answers/programming/sending-email-automatically-by-java/14753.html

java program help www.computing.net/answers/programming/java-program-help/3688.html