• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Date formatting

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey fellow javacoders!

I'll get two Strings from a remote system:
String dateStr = "20080206";
String timeStr = "1215";

I want to convert them into another format:
2008-02-06T12:15:00

What would be the most efficient way of doing this?

Thanks in advance!
 
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.text.SimpleDateFormat
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try to parse them (for example with SimpleDateFormat) and format the resulting Date object again, but that is probably not the most efficient way. You could just use methods in class String to get the parts that you need and paste them together again.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Efficiency is overrated, I think. For most programs, IO/database operations are the slowest by far, and the speed of anything else is unimportant. I would try to make the method as easy as possible to understand and modify later if necessary - to do this, SimpleDateFormat is the way to go. It may be a little bit slower than the method Jesper suggests, but the chance that this will ever matter is remarkably small.
[ February 16, 2008: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to do this in reverse?

e.g. you have a date along the lines of 2008-02-19.

How would you end up with a String with something like 20080219 ??

Thanks
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aaron John:
Is there any way to do this in reverse?


That's really the same problem, isn't it? You can either use SimpleDateFormat or string operations.
 
I am going to test your electrical conductivity with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic