Solved

Parsing the date in required format in java

Posted on 2011-09-29
6
192 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy:unable to resolve class error 2 66
Java / Linux and Regular Expressions 11 68
MySqlDump not dumping triggers 1 13
eclipse with Angularjs2.0 3 14
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

929 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

12 Experts available now in Live!

Get 1:1 Help Now