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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert string to datetime format in pandas python?

Convert string into datetime.time object

how to convert a string date into datetime format in python?

How do I convert a Joda Time DateTime object into a String in SQL Server format?

Convert a MySQL datetime string to time.Time format

Convert given time in String format to seconds in Android

Convert String to DateTime Object and Add it to total time

How to convert datetime from string format into datetime format in pyspark?

How to convert a timestamp string containing date and time in this format, to a PHP DateTime format?

How to convert time object to datetime format in pandas (python)?

Convert time object to datetime format in python pandas

Unity c# convert date-string to datetime object in desired date time format

Convert an unusual/custom time format to datetime object

MySQL convert timestamp and time string to DateTime format

How to convert a string to a datetime format in MySQL

How to convert string to datetime(datetime) in same format?

How to convert a string into a date in a given format

How to convert a string to datetime object

How to convert string to DateTime object with current time in mvc

Given a datetime.time(X,Y) object, how to convert it to time string?

Convert date string format to a datetime Python Object

How to compare date and time, given in string format?

Convert iso format datetime string to datetime object in Python

How to convert string in a datetime.datetime format into a datetime object?

How to convert time string into date and time format?

How to convert string time to datetime

Convert multiple time format object as datetime format

How to convert string timeseries to datetime object in a dataframe while preserving the format of the original datetime?

How to Convert DateTime String to UTC format in Kotlin