Solved

formatting a date

Posted on 2004-04-29
7
242 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

803 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