Skip to main content

unexpected output while converting a string to date in java



I have a string "12/9/2010 4:39:38 PM" which i have to convert to a date object. I am using the following code to do it:







String str = "12/9/2010 4:39:38 PM";



DateFormat formatter ;



Date date ;



formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");



date =(Date)formatter.parse(str);



System.out.println("date printed"+date);







However, when im printing the output, i see







Thu Dec 09 04:39:38 IST 2010







How do I get the date exactly the way I declared in the string i.e







12/9/2010 4:39:38 PM







as output? Pls help


Comments

  1. You're assuming that the Date value itself remembers the format - it doesn't. Date.toString will do what it wants - because the Date only represents an instant in time.

    If you want to format a Date, use your formatter again:

    System.out.println(formatter.format(date));


    However, that won't necessarily return the exact same value that was in your string, as there may be multiple values which parse the same way. For example, as you've only used "H:m:s", I'd expect "4:5:6" to be parsed the same way as "04:05:06".

    ReplyDelete
  2. You can entirely specify the format of your date output using the class Formatter

    Short answer

    String str = "12/9/2010 4:39:38 PM";
    Formatter formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
    Date date =(Date)formatter.parse(str);
    Formatter formatterOutput = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
    String s = formatterOutput.format(date);


    Other examples

    Format formatter;

    // The year
    formatter = new SimpleDateFormat("yy"); // 02
    formatter = new SimpleDateFormat("yyyy"); // 2002

    // The month
    formatter = new SimpleDateFormat("M"); // 1
    formatter = new SimpleDateFormat("MM"); // 01
    formatter = new SimpleDateFormat("MMM"); // Jan
    formatter = new SimpleDateFormat("MMMM"); // January

    // The day
    formatter = new SimpleDateFormat("d"); // 9
    formatter = new SimpleDateFormat("dd"); // 09

    // The day in week
    formatter = new SimpleDateFormat("E"); // Wed
    formatter = new SimpleDateFormat("EEEE"); // Wednesday

    // Get today's date
    Date date = new Date();

    // Some examples
    formatter = new SimpleDateFormat("MM/dd/yy");
    String s = formatter.format(date);
    // 01/09/02

    formatter = new SimpleDateFormat("dd-MMM-yy");
    s = formatter.format(date);
    // 29-Jan-02

    // Examples with date and time; see also
    // Formatting the Time Using a Custom Format
    formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
    s = formatter.format(date);
    // 2002.01.29.08.36.33

    formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
    s = formatter.format(date);
    // Tue, 09 Jan 2002 22:14:02 -0500


    from: http://www.exampledepot.com/egs/java.text/formatdate.html

    ReplyDelete
  3. Use the same formatter:

    System.out.println("date printed "+ formatter.format(date));

    ReplyDelete
  4. Converting it back to String using SimpleDateFormat?

    ReplyDelete
  5. formatter = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
    String temp =formatter.format(date );

    ReplyDelete
  6. Java.util.Date has no concept of an intrinsic format - You need to use the format(java.util.Date d) method to see a formatted String representation of your Date object.

    String str = "12/9/2010 4:39:38 PM";

    DateFormat formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
    Date date =(Date)formatter.parse(str);

    System.out.println("date printed"+formatter.format(date));

    ReplyDelete
  7. Not sure what you are trying to accomplish. But you'll have to call SimpleDateFormat.format() to get what you are expecting. Printing the date directly will get only toString() implementation of Date

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.