?
Solved

Date Format

Posted on 2001-08-01
14
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Technology Partners: 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!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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