?
Solved

Java: Class CALENDAR --> Date conversion

Posted on 2005-03-09
6
Medium Priority
?
816 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

621 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