Date format

S

shoa

Hello
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know
Thank you
S.Hoa
 
A

Andrew Thompson

I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know

'One way', rather than specifically a better way..

// String.split() available in Java 1.4+
String[] dateParts = "2005-06-15".split("-");
StringBuffer sb = new StringBuffer();
for (int ii=dateParts.length-1; ii>0; ii--) {
sb.append( dateParts[ii] + "-" );
}
sb.append( dateParts[0] );
System.out.println( sb );

HTH
 
L

Lasse Reichstein Nielsen

shoa said:
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know

You know the exact format of the input string, so you an use substring
to extract parts:
---
/**
* Converts an ISO date to dd-mm-yyyy format
* @param date An ISO format date.
* @returns The date in dd-mm-yyyy format
*/
String reverseDate(String date) { // e.g. "2005-06-15"
return date.substring(8)) + // "15"
date.substring(4,8) + // "-06-"
date.substring(0,4); // "2005"
}
 
S

shakah

shoa said:
Hello
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know
Thank you
S.Hoa

There are date parsing and formatting methods in the library -- maybe
something like the following would work for you?

public class dateconv {
public static void main(String [] asArgs)
throws java.text.ParseException {
java.text.DateFormat dfYMD =
new java.text.SimpleDateFormat("yyyy-MM-dd") ;
java.text.DateFormat dfDMY =
new java.text.SimpleDateFormat("dd-MM-yyyy") ;

for(int i=0; i<asArgs.length; ++i) {
System.out.println("'" + asArgs + "' => '"
+ dfDMY.format(dfYMD.parse(asArgs)) + "'"
) ;
}
}
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java dateconv "2005-06-15"
'2005-06-15' => '15-06-2005'
 
P

Pete Barrett

Hello
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know

Can't you use the SimpleDateFormat class, and its parse(...) and
format(...) methods?


Pete Barrett
 
W

Wibble

shakah said:
shoa said:
Hello
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know
Thank you
S.Hoa


There are date parsing and formatting methods in the library -- maybe
something like the following would work for you?

public class dateconv {
public static void main(String [] asArgs)
throws java.text.ParseException {
java.text.DateFormat dfYMD =
new java.text.SimpleDateFormat("yyyy-MM-dd") ;
java.text.DateFormat dfDMY =
new java.text.SimpleDateFormat("dd-MM-yyyy") ;

for(int i=0; i<asArgs.length; ++i) {
System.out.println("'" + asArgs + "' => '"
+ dfDMY.format(dfYMD.parse(asArgs)) + "'"
) ;
}
}
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java dateconv "2005-06-15"
'2005-06-15' => '15-06-2005'

All of the above methods work, but thier pretty expensive. Since you
know just where it all goes, and you dont want to create lots of
intermediate objects, or synchronize, the following might be close
to fastest. Copying all chars to a buffer and rearranging might be
faster still, but same idea.

yyyy-mm-dd to dd-mm-yyyy

char buf[] = new char[10];
origString.getChars(0, 4, buf, 6); // copy yyyy
origString.getChars(4, 8, buf, 2); // copy -mm-
origString.getChars(8,10, buf, 0); // dd
String result = new String(buf);
 
R

Ray in HK

I'd rather write a conversion class to convert the string into Date and then
format it.
Unless serious batch processing or system core that required fastest
response, this kind of conversion only consume a tiny bit of CPU time in
reality.

Wibble said:
shakah said:
shoa said:
Hello
I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
format of "dd-mm-yyyy" (For example from 2005-06-15 to 15-06-2005). I can
convert by each char (e.g copy first char (value =2)in the first date string
to the 7th for the second date string, so on.....) . If you know a better
way, please let me know
Thank you
S.Hoa


There are date parsing and formatting methods in the library -- maybe
something like the following would work for you?

public class dateconv {
public static void main(String [] asArgs)
throws java.text.ParseException {
java.text.DateFormat dfYMD =
new java.text.SimpleDateFormat("yyyy-MM-dd") ;
java.text.DateFormat dfDMY =
new java.text.SimpleDateFormat("dd-MM-yyyy") ;

for(int i=0; i<asArgs.length; ++i) {
System.out.println("'" + asArgs + "' => '"
+ dfDMY.format(dfYMD.parse(asArgs)) + "'"
) ;
}
}
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java dateconv "2005-06-15"
'2005-06-15' => '15-06-2005'

All of the above methods work, but thier pretty expensive. Since you
know just where it all goes, and you dont want to create lots of
intermediate objects, or synchronize, the following might be close
to fastest. Copying all chars to a buffer and rearranging might be
faster still, but same idea.

yyyy-mm-dd to dd-mm-yyyy

char buf[] = new char[10];
origString.getChars(0, 4, buf, 6); // copy yyyy
origString.getChars(4, 8, buf, 2); // copy -mm-
origString.getChars(8,10, buf, 0); // dd
String result = new String(buf);
 
S

shoa

Thank you for your help
Other methods are good but the simpleDateFormat is suitable for me.
 
P

P.Hill

I'd rather write a conversion class to convert the string into Date and then
format it.
Unless serious batch processing or system core that required fastest
response, this kind of conversion only consume a tiny bit of CPU time in
reality.

If there is some concern, an intermediate form would be save
the SimpleDateFormat (which contains the Calendar and Timezone
....) and reuse that one object.

-Paul
 
J

John Currier

If there is some concern, an intermediate form would be save
the SimpleDateFormat (which contains the Calendar and Timezone
...) and reuse that one object.

As long as he realizes that SimpleDateFormat isn't thread safe...

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top