Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Using system date in java... How?
suicide
post Sep 11 2004, 01:57 PM
Post #1


Member [ Level 2 ]
Group Icon

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?
Go to the top of the page
 
+Quote Post
JDrive
post Dec 30 2004, 10:39 PM
Post #2


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Jan 30 2005, 11:29 AM
Post #3


PsYcheDeLiC dR3aMeR
Group Icon

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 ::. cool.gif
Go to the top of the page
 
+Quote Post
iGuest
post Sep 15 2008, 03:29 AM
Post #4


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post
pyrotechnique
post Sep 25 2008, 06:27 PM
Post #5


Newbie [ Level 1 ]
Group Icon

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!
Go to the top of the page
 
+Quote Post
iGuest
post Sep 5 2008, 08:28 PM
Post #6


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. java.lang.NullPointException(4)
  2. Java Unlimited(14)
  3. Array Sorting(21)
  4. What Are The Advantages Of Java Vs C++?(15)
  5. Need Help: Find Lowest Character Using Java(7)
  6. Download Java Ebooks(15)
  7. Java By Example(8)
  8. Video Streaming In Web Browser Through Java Or Jsp(1)
  9. Looking For A Java IDE(25)
  10. On Why Java Is 'c'ooler!(10)
  11. Other Sound Format Support(3)
  12. Java Phone Book(2)
  13. How To Create Exe File In Java?(13)
  14. How Do I Test A Java Aplication(11)
  15. Mozilla And Java!(2)
  1. Need To Modify Xml Attribute Using Java(4)
  2. Bluetooth And Java(5)
  3. Java Sdk Vs. Java Jdk?(2)
  4. Graphcal User Interfaces In Java(4)
  5. Java Db Help Pls(2)
  6. Loading 3d Models In Java?(2)
  7. Java Applet Loading Error(5)
  8. Setting Up Java Correctly(8)
  9. Java Java.security.accesscontrolexception(6)
  10. Simple Java Question(3)
  11. Java And Sql: Data Mismatch(6)
  12. Java Memory Leak?(2)
  13. Java Mouse Movement.(2)


 



- Lo-Fi Version Time is now: 2nd December 2008 - 12:53 AM