Solved

Date Format

Posted on 2001-08-01
14
208 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Tomcat: Unable to run tomcat service. 2 23
Desingning Refactoring existing code 2 26
Java Restore security prompts not working 10 50
What browser will run Java? 7 41
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

766 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