Solved

formatting a date

Posted on 2004-04-29
7
240 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
Comment Utility
Use java.text.SimpleDateFormat
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility

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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
> Date myDate = dfIn.parseDate(dateString);

that should be

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

Expert Comment

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

Author Comment

by:D_basham
Comment Utility
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
Comment Utility
=-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now