1

I got String like "Wed, 08 Feb 2012 09:06:41 +0000". I want to fetch "Date" 08-Feb-2012 and "Time" 09:06.

How can I get it?? Please Help me.

Thanks in advance..:)

2

7 Answers 7

3

Try This,

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
try 
{
    Date date = (Date)sdf.parse("Wed, 08 Feb 2012 09:06:41 +0000");
    System.out.println(date);
    SimpleDateFormat day = new SimpleDateFormat("dd-MMM-yyyy");
    SimpleDateFormat time = new SimpleDateFormat("HH:mm");
    System.out.println(day.format(date));
    System.out.println(time.format(date));
} 
catch (ParseException e) 
{
    e.printStackTrace();
}

It will work for sure.

Sign up to request clarification or add additional context in comments.

Comments

1
Date d = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse(yourString);

Comments

1

You can use SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

You just need a correct format:

"EEE, d MMM yyyy HH:mm:ss Z"

1 Comment

@Hardik: if you omit Z then parser will ignore time zone in string and will not produce exact time. You see "correct" result, but resulting date object is not same as one that produced your string. I think you need to go deeper and understand how time stored in Java. Here is good article about this.
0
DateFormat formatter ; 
Date date ; 
formatter = new SimpleDateFormat("EEE, dd MMM yyy HH:mm:ss Z");
date = (Date)formatter.parse(str_date);  

look here http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for date and time patterns.

Comments

0

Use following idiom:

DateFormat fmt = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date date = (Date)fmt.parse("Wed, 08 Feb 2012 09:06:41 +0000");

"E, dd MMM yyyy HH:mm:ss Z" it's a pattern for your parser.

"Wed, 08 Feb 2012 09:06:41 +0000" actually your date string.

Now date object contains structured date and you can extract all that you need.

Comments

0

You can use SimpleDateFormat

To display 08-Feb-2012 you can SimpleDateFormat as

SimpleDateFormat  m_sdFormatter = new SimpleDateFormat("dd-MMM-yyyy");  
m_sdFormatter.format(your date);

To display "Time" 09:06. you can SimpleDateFormat as

SimpleDateFormat  m_sdFormatter = new SimpleDateFormat("HH:mm");  
m_sdFormatter.format(your date);

Comments

0

First parse the string into a Date, then format the date into the strings you need:

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    try {
        Date date = (Date)sdf.parse("Wed, 08 Feb 2012 09:06:41 +0000");
        System.out.println(date);
        SimpleDateFormat day = new SimpleDateFormat("dd-MMM-yyyy");
        SimpleDateFormat time = new SimpleDateFormat("HH:mm");
        System.out.println(day.format(date));
        System.out.println(time.format(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }

outputs:

Wed Feb 08 09:06:41 GMT 2012
8-Feb-2012
09:06

2 Comments

I got date.But,It gives time 14:36..??
@Hardik: That's correct. Source string says "+0000" time zone. Parsed date will displayed in your local time zone (as .toString() invocation). But in fact it is the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.