Solved

Parsing the date in required format in java

Posted on 2011-09-29
6
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

627 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