Solved

formatting a date

Posted on 2004-04-29
7
245 Views
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
0
Comment
Question by:D_basham
[X]
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
7 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10954194
Use java.text.SimpleDateFormat
0
 
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);
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 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('/');
strbuf.append(dateString.substring(6, 8));
strbuf.append('/');
strbuf.append(dateString.substring(0, 4));
String newlyFormattedDateString = strbuf.toString();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

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

that should be

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

Expert Comment

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

Author Comment

by:D_basham
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. :)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10963944
=-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

705 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