Solved

Parsing the date in required format in java

Posted on 2011-09-29
6
193 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 78
Java Loop 6 62
iterator/ListIterator approach 17 28
throw exception 21 44
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

831 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