The moose likes Java in General and the fly likes Date formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date formatting" Watch "Date formatting" New topic

Date formatting

Asrita Saran

Joined: Jul 11, 2007
Posts: 14
I am facing some problem while formatting the date.

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/DD");
java.util.Date d=sdf.parse(Edate.substring(0,9));
Date nd=new Date(sdf.format(d));

Im getting EDate value from a service in the format yyyy-mm-dd.
I have to set this date value to another method ,so im trying to convert the string to date.
When i try to print d and nd values ,it is not printimg the value i retrieved and it is printimg in the format dd/mm/yy.
What can i do to get the correct date and in the same format as yyyy/mm/dd?

Thanks in advance.
jeya prabha

Joined: Jun 03, 2008
Posts: 23
you can use DateFormat class in java.text package
for example
DateFormat df = DateFormat.getDateInstance();
for (int i = 0; i < myDate.length; ++i) {
output.println(df.format(myDate[i]) + "; ");
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
If the date you are reading uses '-' as a separator, why are you trying to reading it with a pattern that uses '/' as a separator ?

jeya prabha

Joined: Jun 03, 2008
Posts: 23
try this
import java.text.*;
import java.util.*;
class Dates3 {
public static void main(String[] args) {
Date d1 = new Date(1000000000000L);

DateFormat[] dfa = new DateFormat[6];
dfa[0] = DateFormat.getInstance();
dfa[1] = DateFormat.getDateInstance();
dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT);
dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dfa[4] = DateFormat.getDateInstance(DateFormat.LONG);
dfa[5] = DateFormat.getDateInstance(DateFormat.FULL);

for(DateFormat df : dfa)
Asrita Saran

Joined: Jul 11, 2007
Posts: 14
Sorry..i am using '-' as seperator in simpledateformat..Im getting one day less as result in the format dd/mm/yy
[ September 02, 2008: Message edited by: Asrita Saran ]
subject: Date formatting
It's not a secret anymore!