?
Solved

Java: Class CALENDAR --> Date conversion

Posted on 2005-03-09
6
Medium Priority
?
796 Views
Last Modified: 2008-03-10
Hi,
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
0
Comment
Question by:go4java
[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
  • 4
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13496332
Use SimpleDateFormat
0
 

Author Comment

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

Accepted Solution

by:
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) {
}
0
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

by:CEHJ
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) {
      e.printStackTrace();
}    
0
 
LVL 37

Expert Comment

by:zzynx
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;
    }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13496551
Thanks
0

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