Solved

Date Format

Posted on 2001-08-01
14
205 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now