Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Date format

Thread view: 
shoa - 04 Jun 2005 14:18 GMT
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
Andrew Thompson - 04 Jun 2005 14:55 GMT
> 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

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Lasse Reichstein Nielsen - 04 Jun 2005 16:19 GMT
> 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"
}
---

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

shakah - 04 Jun 2005 16:25 GMT
> Hello
> I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
[quoted text clipped - 4 lines]
> 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[i] + "' => '"
           + dfDMY.format(dfYMD.parse(asArgs[i])) + "'"
         ) ;
     }
   }
 }

 jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java dateconv "2005-06-15"
 '2005-06-15' => '15-06-2005'
Wibble - 04 Jun 2005 17:14 GMT
>>Hello
>>I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
[quoted text clipped - 26 lines]
>   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);
Ray in HK - 04 Jun 2005 18:42 GMT
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.

¤¤¼¶¼g...

> >>Hello
> >>I have a date in the format of "yyyy-mm-dd". Now I want to show it in the
[quoted text clipped - 40 lines]
> origString.getChars(8,10, buf, 0); // dd
> String result = new String(buf);
P.Hill - 07 Jun 2005 17:42 GMT
> "Wibble" <Wibble@Mailinator.com>
>>
>>All of the above methods work, but thier pretty expensive.

> 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
John Currier - 08 Jun 2005 03:50 GMT
> 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
Pete Barrett - 04 Jun 2005 17:00 GMT
>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
shoa - 05 Jun 2005 04:13 GMT
Thank you for your help
Other methods are good but the simpleDateFormat is suitable for me.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.