Unparseable date: "2017-01-02T01:41:24Z" (at offset 10) - android

2017-01-02T01:41:24Z is my actual date format and I want to convert this date in yyyy-MM-dd hh:mma format.
Please see the following code I tried so far,
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
Date date = null;
try
{
date = sdf.parse(newsDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
but at date = sdf.parse(newsDate); line I'm getting the following error:
"Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)".
Please guide me, where could i have gone wrong?

Because you are using different Date Format which is not correct.
Change this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
to this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

Try this:
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(newsDate)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}

This works perfectly for me.
String dateNew= "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(dateNew)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}

The other answers are wrong with respect to time zone / offset - interpretation. The trailing "Z" denotes UTC+00:00 and must not be interpreted just as literal. The documentation of Android defines the suitable pattern symbol X which can handle this kind of input. If you don't care about this detail then you will not get an exception but wrong data (which is even worse).
So the final solution including the fix for the hour-part looks like:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Note: Use XX instead of XXX if you have offsets without colon.

Related

How to get the date from "2019-06-27T12:30:00.000+0000"?

I want to get date and time from 2019-06-27T12:30:00.000+0000 in android. I tried a code but it is not working.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = null;//You will get date object relative to server/client
timezone wherever it is parsed
try {
date = dateFormat.parse("2019-06-27T12:30:00.000+0000");
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
String dateStr = formatter.format(date);
You're missing out on parts of the Date
The pattern you need is,
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'ssss");
You're missing the millisecond portion of your SimpleDateFormat, below is what you need for that specific pattern:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

String date to milliseconds

I have a date String (format: dd-mmm-yyyy) and I would like to convert it into milliseconds, but it has to have the time zone of the device.
I've already tried this, but unfortunetly the "f.parse("17-July-2018");" is not working right.
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); //Sample "17-July-2018"
try {
Date d = f.parse("17-July-2018");
bonusTime = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
The parsing format for full month names is MMMM
SimpleDateFormat f = new SimpleDateFormat("dd-MMMM-yyyy", Locale.ENGLISH);
should work. MMM is for short names like "Jan" or "Feb".

Android dateformat display format invalid

This might be easy, and I might no see the obvious, but I'm like 1 hour trying to figure out how to convert the date from string to a format I want.
String date_begin = "15-10-2013 10:25:31";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // This is the format I want
Date startDate;
try {
startDate = df.parse(date_begin);
String startDateString1 = df.format(startDate);
Log.e("New date: ", startDateString1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And the output is the following:
New date:(2799): 0021-04-04 10:25:31
How d'hell is displaying this output?
15-10-2013 10:25:31 is not in yyyy-MM-dd HH:mm:ss format. Parse it with dd-MM-yyyy HH:mm:ss instead.
Looks like you want to change the formatting. The solution is to use two SimpleDateFormats, one for parsing and another for formatting the output. Like this:
DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
startDate = inputFormat.parse(date_begin);
String startDateString1 = outputFormat.format(startDate);
Log.e("New date: ", startDateString1);
you have to parse the date with a different format
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
The format of the date_begin , SimpleDateFormat("yyyy-MM-dd HH:mm:ss") is not same . Both should be in same format.

how to format 2013-05-15T10:00:00-07:00 to Date android

I am trying to format the date string to Date, and then get the month/day from it:
String strDate="2013-05-15T10:00:00-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss-z");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday= sdfmonth.format(convertedDate);
but it returns me current month/day i.e 5/18. Whats wrong?
3 things :
There is a mistake in your format : 2013-05-15T10:00:00-07:00 has no meaning, it should be 2013-05-15T10:00:00-0700 (with no colon at the end, this is a timezone defined in RFC 822. (Look at the docs about Z).
Change your format to yyyy-MM-dd'T'HH:mm:ssZ as mentionned by #blackbelt
you get a bad date because you re-format your date whatever happens during parsing. Re-format in your try block, if and only if parsing worked.
----------Update
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday = sdfmonth.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
I don't know what is wrong in your code. for me it throws Unparseable exception like this.
java.text.ParseException: Unparseable date: "2013-05-15T10:00:00-07:00"
But the following way works well.
String strDate="January 2, 2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Date date = dateFormat.parse(strDate);
System.out.println(date.getMonth());
But in java Date is deprecated as per http://docs.oracle.com/ . Try to use Calender instead of Date.
I hope this will help you.

Android Date format conversion 71

I have a date in 02/21/2013 04:52:10 PM this format.
How do I convert it into MM-dd-yyyy HH:mm. I already tried few things but it keeps throwing error
java.text.ParseException: Unparseable date: "02/21/2013 04:52:10 PM" (at offset 2)
I really need a help from date format expert.
Thanks
Try this one:
DateFormat inputFormat = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a", Locale.getDefault());
DateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm", Locale.getDefault());
try {
System.out.println("Converted: " + outputFormat.format(inputFormat.parse("02/21/2013 04:52:10 PM")));
} catch (ParseException e) {
e.printStackTrace();
}
Output: 01-21-2013 16:52
Checkout http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more
Cheers
Construct two SimpleDateFormat objects. The first you parse() the value from into a Date object, the second you use to turn the Date object back into a string, e.g.
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy");
return df2.format(df1.parse(input));
}
catch (ParseException e) {
return null;
}
Parsing can throw a ParseException so you would need to catch and handle that.
In addition, check this out:-
http://developer.android.com/reference/java/text/SimpleDateFormat.html
and more:-
http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx
and more:-
Date format conversion Android
You need verify if your String have / or - in the mask, case have / then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
case your String have - then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
Or then, use the replace() for change of the caracter - for /

Categories

Resources