Java: Class CALENDAR --> Date conversion

Posted on 2005-03-09
Medium Priority
Last Modified: 2008-03-10
I'm looking for a simple way to convert a date in a certain format to another date in a certain format.
Example: Input format = ddMMMyyyy Output format = yyyyMMdd
Question by:go4java
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
  • 4
LVL 37

Expert Comment

ID: 13496332
Use SimpleDateFormat

Author Comment

ID: 13496347
I know. Is there something generic code available?
LVL 37

Accepted Solution

zzynx earned 1000 total points
ID: 13496357
String date = "02Mar2005";
SimpleDateFormat f1 = new SimpleDateFormat("ddMMMyyyy");
SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMdd");

try {
   Date d = f1.parse(date);
   System.out.println( f2.format(d) );
} catch (ParseException ex) {
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

LVL 86

Expert Comment

ID: 13496367
try {
      DateFormat df1 = new SimpleDateFormat("ddMMMyyyy");
      DateFormat df2 = new SimpleDateFormat("yyyyMMdd");
      Date d1 = df1.parse(stringInput1);
      String newFormat = df2.format(d1);
catch(Exception e) {
LVL 37

Expert Comment

ID: 13496385
Or if you want:

public String getOtherFormat(String format1, String date, String format2) {

    SimpleDateFormat f1 = new SimpleDateFormat(format1);
    SimpleDateFormat f2 = new SimpleDateFormat(format2);

    try {
       Date d = f1.parse(date);
       return f2.format(d);
    } catch (ParseException ex) {
        return null;
LVL 37

Expert Comment

ID: 13496551

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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