How to format the given time string and convert to date/time object

Anupam

I am getting the time object in the form of a string from a rest service . I need to extract the time and then do some time operation.The given time string is "2015-06-16T14:58:48Z". I tried the below code , to convert the string to the time , however , getting incorrect values.

    String time = "2015-06-16T14:58:48Z";

    SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss'Z'", Locale.US);

    String dateInString = "2015-06-16T14:58:48Z";

    Date date = formatter.parse(dateInString);
    System.out.println("Original String : " + time);
    System.out.println("After converting to time : " + formatter.format(date));

The output that i am getting is as below: Original String : 2015-06-16T14:58:48Z After converting to time : 2015-12-362T02:58:48Z

The converted date somehow is getting wrong value.Please suggest where is the mistake.Thanks.

Mureinik

You format string has a couple of mistakes:

  • Y means the week year, not the year, which is y
  • D means the day of the year. You should have used d, which means the day of the month.
  • h means a 12-hour notation time of day. Since you have 14 you should use H, which handle a 24-hour notation.

To sum it all up:

SimpleDateFormat formatter = 
    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert string data with variable format to datetime?

分類Dev

How do I convert a Calendar object to an ISO 8601 format DateTime string?

分類Dev

How to convert date into given format?

分類Dev

Java Convert String into datetime Object

分類Dev

What is datetime object in Google API, and how to format its string output?

分類Dev

How to convert DateTime to a String

分類Dev

How to convert string format of time type field in DolphinDB c ++ API

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

分類Dev

Object must be a Date, DateTime or Time object. nil given

分類Dev

Convert a specific format dateTime string to DateTime in C#

分類Dev

How can I convert date time format string used by C# to the format used by moment.js?

分類Dev

Convert string to time format access 2007 query

分類Dev

convert text string to date/time format

分類Dev

PHP - Convert this string to another date/time format

分類Dev

Converting a time string to append with DateTime object

分類Dev

How to convert a datetime format to minutes - pandas

分類Dev

How to convert a column with null values to datetime format?

分類Dev

How to convert this STRING into time + date

分類Dev

How do I convert API supplied date/time string into readable format?

分類Dev

SQL DateTime Format Convert

分類Dev

How to convert object into string in javascript?

分類Dev

How to convert a String to Object in Flutter?

分類Dev

How do I get the current date and time in the format given Below

分類Dev

how to convert python time.time() to date format for sql insertion

分類Dev

How to convert string into a date format in spark

分類Dev

How to convert string to dig readable format rails?

分類Dev

How to convert oracle number type to string with format?

分類Dev

How to convert given html string to normal string android?

分類Dev

How to convert to datetime if the format of dates changes gradually through the column?

Related 関連記事

  1. 1

    How to convert string data with variable format to datetime?

  2. 2

    How do I convert a Calendar object to an ISO 8601 format DateTime string?

  3. 3

    How to convert date into given format?

  4. 4

    Java Convert String into datetime Object

  5. 5

    What is datetime object in Google API, and how to format its string output?

  6. 6

    How to convert DateTime to a String

  7. 7

    How to convert string format of time type field in DolphinDB c ++ API

  8. 8

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  9. 9

    Object must be a Date, DateTime or Time object. nil given

  10. 10

    Convert a specific format dateTime string to DateTime in C#

  11. 11

    How can I convert date time format string used by C# to the format used by moment.js?

  12. 12

    Convert string to time format access 2007 query

  13. 13

    convert text string to date/time format

  14. 14

    PHP - Convert this string to another date/time format

  15. 15

    Converting a time string to append with DateTime object

  16. 16

    How to convert a datetime format to minutes - pandas

  17. 17

    How to convert a column with null values to datetime format?

  18. 18

    How to convert this STRING into time + date

  19. 19

    How do I convert API supplied date/time string into readable format?

  20. 20

    SQL DateTime Format Convert

  21. 21

    How to convert object into string in javascript?

  22. 22

    How to convert a String to Object in Flutter?

  23. 23

    How do I get the current date and time in the format given Below

  24. 24

    how to convert python time.time() to date format for sql insertion

  25. 25

    How to convert string into a date format in spark

  26. 26

    How to convert string to dig readable format rails?

  27. 27

    How to convert oracle number type to string with format?

  28. 28

    How to convert given html string to normal string android?

  29. 29

    How to convert to datetime if the format of dates changes gradually through the column?

ホットタグ

アーカイブ