Solved

Parsing the date in required format in java

Posted on 2011-09-29
6
191 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

18 Experts available now in Live!

Get 1:1 Help Now