|
|
|
|
![]() ![]() |
Sep 11 2004, 01:57 PM
Post
#1
|
|
|
Member [ Level 2 ] Group: Members Posts: 55 Joined: 7-September 04 Member No.: 351 |
How do u use system date in java?
I only know how in jsp. In jsp, u need to do the code below to get the sysdate <%@ page import="java.text.*, java.util.Date" %>String date = DateFormat.getInstance().format(new Date()); and you will be able to get the system date which has the format 6/6/04 7:19 AM ______________________ iv tried to do this in java import java.util.Date;public String getSysDate() { String date = ""; String date = DateFormat.getInstance().format(new Date()); return date; } there's an error on the variable DateFormat., it cannot resolve symbol. does anyone know how? |
|
|
|
Dec 30 2004, 10:39 PM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 6 Joined: 30-December 04 Member No.: 1,935 |
QUOTE(suicide @ Sep 11 2004, 02:57 PM) How do u use system date in java? I only know how in jsp. In jsp, u need to do the code below to get the sysdate <%@ page import="java.text.*, java.util.Date" %>String date = DateFormat.getInstance().format(new Date()); and you will be able to get the system date which has the format 6/6/04 7:19 AM ______________________ iv tried to do this in java import java.util.Date;public String getSysDate() { String date = ""; String date = DateFormat.getInstance().format(new Date()); return date; } there's an error on the variable DateFormat., it cannot resolve symbol. does anyone know how? That's because the import for the DateFormat is missing; DateFormat is not in default package (java.lang) so you have to import it as you did it in your jsp either by CODE import java.text.*; or by CODE import java.text.DateFormat; in the import section then it should work.... Cheers... JDrive |
|
|
|
Jan 30 2005, 11:29 AM
Post
#3
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 myCENTs:84.36 |
QUOTE import java.util.Date;public String getSysDate() { String date = ""; String date = DateFormat.getInstance().format(new Date()); return date; } --> The last post explained successfully why your code couldn't find the command as you didnot declare the exclusive namespace of the package. Another problem your could face here is casting the date from Date type to String. You have to use the toString() method which forms a part of every cannonical class in java. Try the following code instead: ========================================== import java.util.Date; public string getSysDate { Date sysDate = new Date (); String sysDateStr = sysDate.toString (); return sysDateStr; } ========================================== There are several other methods to perform the same. Here's the code: import java.text.SimpleDateFormat; import java.util.Calendar; public string getSysDate { Calendar sysDate; String sysDateStr; sysDate = Calendar.getInstance(); sysDateStr = new SimpleDateFormat("dd-mmm-yy").format(curDate).toString(); return sysDateStr; } ========================================== Yet, a third method: import java.text.SimpleDateFormat; import java.util.Date; public string getSysDate { // Make sure the Month ("MM") in the "MM-dd-yyyy" is in CAPITALS SimpleDateFormat DateFormat = new SimpleDateFormat("MM-dd-yyyy"); Date today = new Date(); return DateFormat.Format (today).toString (); } Any of them should do your job fine.... .:: Cheers ::. |
|
|
|
Sep 15 2008, 03:29 AM
Post
#4
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
Get the Locale & DateTime
Using system date in java... How? Package org.Glen.Helloworld; Import java.Util.Date; Import java.Util.Locale; Import java.Text.DateFormat; Public class HelloMain { public static void main(String[] args) { System.Out.Println("Locale: " + Locale.GetDefault()); System.Out.Println("DateTime: " + DateFormat.GetInstance().Format(new Date())); } } -reply by glen |
|
|
|
Sep 25 2008, 06:27 PM
Post
#5
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 1 Joined: 25-September 08 Member No.: 32,890 |
The key here is that "new Date()" returns the system date. How you want to format it is up to you, but any of the above mentioned suggestions are good!
|
|
|
|
Sep 5 2008, 08:28 PM
Post
#6
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
Current Sysdate using Java
Using system date in java... How? Import java.Util.Date; import java.Text.DateFormat; import java.Text.SimpleDateFormat; private String getDateTime() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.Ms"); Date date = new Date(); System.Out.Println(dateFormat.Format(date)); } You can format the date as per your requirement. Here I have tried to print the milliseconds also...Keep Rocking!! -reply by niranjan |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 2nd December 2008 - 12:53 AM |