[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date Format

Posted on 2001-08-01
14
Medium Priority
?
216 Views
Last Modified: 2010-03-31
I am getting a time in this format

Wed Aug 01 16:32:15 HKG 2001  when I use

java.util.Date d = new java.util.Date() ;
String sdate1=d.toString();

How can I replace the HKG with GMT.


Also, how can I get a time format in say

2001:08:01:16:00:03    format


What kind of format is best for SQL  too ???
or perhaps where is the best place to look for all this formatting answers?

0
Comment
Question by:TLTEO
  • 6
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:ibro
ID: 6343626
There are a couple of classes for data format. Look at classes DateFormat and SimpleDateFormat. I usually use SimpleDateFormat.

SimpleDateFormat df= new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss")

System.out.println("Current date time:"+df.format(new java.util.Date(System.currentTimeMillis())));

hope this helps
0
 
LVL 92

Expert Comment

by:objects
ID: 6343835
In 1.1 you can use:

d.toGMTString();

It's derecated in 1.2 in favour of:

DateFormat.format() using a GMT locale.


0
 
LVL 92

Expert Comment

by:objects
ID: 6344034
Woops, I meant GMT timezone:

formatter = DateFormat.getDateTimeInstance();
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String s = formatter.format(d);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Recall
ID: 6344128
For SQL, you can use the java.sql.Date instead of java.util.Date
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 6347404
I think the format u want can get using the SimpleDateFormat class as irbo specified. If u couldn't get try this one,

 Use Calendar class for assigning and getting the date.

 In calendar class u have the functionallity to get the day, month, year, hour, minutets, and seconds. Get these things using the functionalities and arrange it as u want in a string.

 While storing the date u wont get any error if u assignthe value in the query string itself as a string, but if u used the statement obj and use the set*****() method to set the value. U should go for java.sql.Date not the util Date. the sql format will be mm-dd-yyyy. So u need to convert the date format to this and then assign it to the sql Date.

If u have any doubt post it.
thank u
siva
0
 
LVL 92

Expert Comment

by:objects
ID: 6347834
> So u need to convert the date format to this and
> then assign it to the sql Date.

java.sql.Date is simply a subclass of Date and holds no format info that I'm aware of.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 6351321
> So u need to convert the date format to this and
> then assign it to the sql Date.

>>java.sql.Date is simply a subclass of Date and holds no >>format info that I'm aware of.

Check it by printing.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 6351331
Sorry Mike,

use this way

 java.sql.Date sqldate1 = new java.sql.Date(arrDateTmp.getTime().getTime());

  Here, arrDateTmp is a Calendar obj
0
 

Author Comment

by:TLTEO
ID: 6352925
How shd I do it??

I specify a Calendar object ..(how to?)

and then

 java.sql.Date sqldate1 = new java.sql.Date(arrDateTmp.getTime().getTime());

Can this sqldate1 be compared??  such that eg
 if (date1 > 08:06:2001)
{
...
deleterecord(date1)
}

0
 
LVL 2

Expert Comment

by:vasan_sr
ID: 6353915
hi,
  for all date format u can use the following piece of code. U can change the format of date in either way u want.

java.text.SimpleDateFormat date_format  = new java.text.SimpleDateFormat ("dd-MMM-yy");
java.util.Date  dt1= new java.util.Date();
String t=(date_format.format(dt1));

this will sort u r problem of date
0
 
LVL 92

Expert Comment

by:objects
ID: 6354542
That won't display GMT time.
0
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 6354545
In 1.1:

d.toGMTString();

OR in 1.2:

formatter = DateFormat.getDateTimeInstance();
// Or you can explicity create a SimpleDateFormat
// if you need to use a specialised format.
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String s = formatter.format(d);

0
 

Author Comment

by:TLTEO
ID: 6354574
objects

how to compare dates?
0
 
LVL 92

Expert Comment

by:objects
ID: 6354854
Use the before(), after() and equals() methods of Date.

Thanks for the points :)

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question