Solved

Parsing the date in required format in java

Posted on 2011-09-29
6
195 Views
Last Modified: 2012-05-12
Hi experts...
I have date specified in the following format in the 5th line of a file.as follows inclusive of quotes-
"SEPTEMBER 20, 2011 - SEPTEMBER 28, 2011"
How do i get this two dates as start and end date in two variables in the format
ddMMMYYYY like startdate as 20SEP2011 and end date as 28SEP2011 as per the given example.
Please help...
0
Comment
Question by:gaugeta
  • 4
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36814207
first split the string with delimiter as '-'
second, use simple date format MMMMMMMMM dd, yyyy

see this as reference
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814228
Try the following:
public static Date[] getRange(String s) {
        Date[] result = new Date[2];

        try {
            DateFormat df = new SimpleDateFormat("MMMM dd, yyyy");
            String[] sDates = s.toLowerCase().split("\\s*-\\s*");
            System.out.println(Arrays.toString(sDates));
            result[0] = df.parse(sDates[0]);
            result[1] = df.parse(sDates[1]);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814229
>>System.out.println(Arrays.toString(sDates));

(can go)
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:gaugeta
ID: 36814312
@CEHJ:After trying this i got the result as
Start date is Tue Sep 20 00:00:00 EDT 2011
End date is Wed Sep 28 00:00:00 EDT 2011
I just wanted start date as 20SEP2011 and end date as 28SEP2011 with month in capital letters.
Please help...
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 36814334
You can format the Date instances however you like. You need them as type Date to work with them. For display purposes, just format them with
DateFormat df = new SimpleDateFormat("ddMMMyyyy");
String s = df.format(yourDateReference).toUpperCase();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814361
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
Facing this issue for maven proxy setting 2 25
spring maven example issues 3 37
Html split(text) 2 25
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

679 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