Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


formatting a date

Posted on 2004-04-29
Medium Priority
Last Modified: 2012-05-04
Good afternoon everyone, I have probably a real simple question but I am not very clear on how to do it. I need to format a date that is comming into my program as "200404271127" And I need take that date and make it into this format 04/27/2004. Any ideas? Thanks
Question by:D_basham
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
LVL 14

Expert Comment

by:Tommy Braas
ID: 10954194
Use java.text.SimpleDateFormat
LVL 14

Expert Comment

by:Tommy Braas
ID: 10954214

String dateString = ....
SimpleDateFormat dfIn = new SimpleDateFormat("yyyyMMddHHmm");
Date myDate = dfIn.parseDate(dateString);
SimpleDateFormat dfOut = new SimpleDateFormat("MM/dd/yyyy");
String newFormattedString = dfOut.format(myDate);
LVL 14

Accepted Solution

Tommy Braas earned 500 total points
ID: 10954230
Of course, if the format is fixed then the substring approach would work as well:
String dateString = ....
StringBuffer strbuf = new StringBuffer();
strbuf.append(dateString.substring(4, 6));
strbuf.append(dateString.substring(6, 8));
strbuf.append(dateString.substring(0, 4));
String newlyFormattedDateString = strbuf.toString();
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 92

Expert Comment

ID: 10954391
> Date myDate = dfIn.parseDate(dateString);

that should be

Date myDate = dfIn.parse(dateString);
LVL 14

Expert Comment

by:Tommy Braas
ID: 10954525
>> Date myDate = dfIn.parse(dateString);
of course....;-)

Author Comment

ID: 10959192
Thanks for the help the substring parsing is what I needed and it worked. Stick around I am doing a lot of java programming for work may come up with more questions. Thanks a lot. :)
LVL 14

Expert Comment

by:Tommy Braas
ID: 10963944

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 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