Hardik
Hardik

Reputation: 1411

How to fetch date and time from String in Java?

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..:)

Upvotes: 1

Views: 2590

Answers (7)

Pratik Patel
Pratik Patel

Reputation: 474

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.

Upvotes: 3

dave.c
dave.c

Reputation: 10918

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

Upvotes: 0

AndroidDev
AndroidDev

Reputation: 4559

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);

Upvotes: 0

mishadoff
mishadoff

Reputation: 10789

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.

Upvotes: 0

Sapan Diwakar
Sapan Diwakar

Reputation: 10966

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"

Upvotes: 1

lxbndr
lxbndr

Reputation: 2208

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

Upvotes: 1

dhblah
dhblah

Reputation: 10161

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.

Upvotes: 0

Related Questions