views:

54

answers:

2

I want to parse date of following type:

2010-07-13T17:27:00.000Z

How can i do it using simple date formatter in java? what format is to be used?

+5  A: 
  1. Take a look at the javadocs of SimpleDateFormat.
  2. Create an instance of this class, using the appropriate String in the constructor
  3. Call its parse method, passing in the String in your question
  4. ???
  5. Profit!

(You may notice that I'm not actually giving you the format string. This is a "teach a man to fish" answer. If you have problems working out specifically what you'd need to use for a particular section, then feel free to elaborate, stating what you tried and why it didn't work. But right now it sounds like you haven't got to the point of attempting any specific format strings. The Javadocs are reasonably well-written and contain everything you need. Being able to extract information from documentation is a massively important skill for a programmer and I'm not going to rob you of a chance to improve on it.)

Andrzej Doyle
i have a text file containing this kind of time stamp.I want toread it
aks
Excellent. What happens when you follow the above steps (note, steps 4 and 5 are optional really)?
Andrzej Doyle
+1 for "teaching a man to fish" :-)
bhangm
Don't you mean to say *Call its `parse` method* in step 3?
BalusC
Oops, of course. Corrected now, thanks for pointing that out.
Andrzej Doyle
+1  A: 

The code should look like the following code.

For your date string "2010-07-13T17:27:00.000Z" you may try this format "yyyy-MM-dd'T'hh:mm:ss.S'Z'".

I assume the 'T' and 'Z' in your date string is constant/separator only.

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestMain {

    public static void main(String[] args) throws Exception{

        String fromDateTime = "2010-12-01 00:01:23";
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
        Date date = null;

        date = format.parse(fromDateTime);
        //What ever you want to manipulate of this date object
        //... 

    }
}

EDIT: add proper class, method & comment to make it a complete program. Thanks for comment from @Andrzej Doyle. EDIT: remove throws IOException from the demo program. Thanks for @BalusC. EDIT: re-read the comment, got the full meaning of @BalusC :)

ttchong
Give you the fish :)
ttchong
I'm really not keen on example bits of code that catch exceptions and merely print the stack trace. For the majority of applications, this is equivalent to simply swallowing the exception. The problem is that a simple copy-and-paste means that the code "works", so likely no attention is paid to the details of error handling. I **much** prefer code snippets that put this in a method and simply declare the method to throw the relevant Exception - this is both correct and forces copy-pasters to really think about what they want to do with the exception. (And if (*they* swallow it, so be it).
Andrzej Doyle
@Andrzej Doyle- for production code yes, you list one by one the discrete exception that you want to handle and ignore the one you don't care exactly. For quick demo or debugging code, personally I prefer to print whatever exception being throw simply because it causes me less key stroke to answer a question. During debug session, it is also easier for you to get a working code first before you start to care on those possible bad things to happen but you don't see it through out the time you spend with your code. Refactoring always has it beauty :)
ttchong
Replace `throws IOException` (was it a leftover? ;) ) by `throws Exception`, get rid of the `catch` blocks and it'll be fine. Let the "enduser" worry about exception handling himself.
BalusC
@BalusC - done. thanks.
ttchong