Solved

Date Format

Posted on 2001-08-01
14
209 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 20 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 73
Java Restore security prompts not working 10 131
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 43
Java List 4 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

749 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