How to paste date on desire format Android? - android

What I am trying to do is get the correct date format in a String.
I got it like this: 2020-05-13T22:00:52+0000
The desire one would be May-13-2020
I tried this:
Date date = new Date();
String insuranceRequestDate = formatDate.format(date);
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
//java.util.Date date1 = null;
try
{
date = form.parse(insuranceRequestDate);
}
catch (ParseException | java.text.ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy",Locale.getDefault());
String newDateStr = postFormater.format(date);
But I get an error:
java.text.ParseException: Unparseable date: "2020-05-13T22:00:52+0000" (at offset 19).
Any help or suggestions would be great. By the way, I canĀ“t understand the answers to other questions in case you wonder if there are similar questions, thanks!

You can try this format:
"yyyy-MM-dd'T'HH:mm:ssXX"
instead of
"yyyy-MM-dd'T'HH:mm:ss.SSS"
yyyy-mm-dd'T'HH:MM:SSXX => 2020-04-26T22:12:38+0200
yyyy-mm-dd'T'HH:MM:SS.SSS => 2020-04-26T22:12:38.226
So you are getting ParseException

I solved it actually with a much simpler solution:
Date date = new Date();
String insuranceRequestDate = DateFormat.getDateInstance().format(date);

Related

How to convert Api String date to mm-dd-yy date format (Android) without the use of datepicker?null Pointer exception

I am getting Time from an api in this format
"2018-03-07T11:19:54.686Z" ..... How to convert this String data to mm-dd-yyyy date format in android.
SimpleDateFormat format = new SimpleDateFormat(" MM,dd,yy");
String date = format.format(Date.parse(data));
I have tried using the above two but there is a parse error.
First you have to parse the given date "2018-03-07T11:19:54.686Z" in the same format and then you can apply formatting with the desired format.
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = format.format(parseDateFormat.parse(data));
you used below code and show your format according to date.
String date="2018-03-07T11:19:54.686Z";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateFormat formatdate = new SimpleDateFormat("MM,dd,yy");//if show mm-dd-yy
try {
Date date1=parseDateFormat.parse(date);
Log.d("New Date",formatdate.format(date1));
} catch (ParseException e) {
e.printStackTrace();
}

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

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.

Completely reformat a String? - Android

Currently i am pulling data from an API and loading it into my App. It comes in JSON and a particular string is very ugly to look at. I'm looking to be able to rearrange/reformat this String to make it more pleasing. It's a date/time String.
I've had a look around and have found a few different possibilities, but it's not efficient at all.
Example of what i get, and what i'm after...
JSON: 2016-06-23T02:55:14.907
Formatted: 23/06/2016 - 02:55
Anything along those lines, or just in general. I'm just looking for suggestions of ways i might be able to do it. I'm still fairly new to Android. Anything you can give me would be greatly appreciated.
EDIT 28/06/2016
Here is what i'm doing
dateJSON is: 2016-06-01T21:00:00
public String convertDate(String dateJSON) {
String convertedDate = "";
Format formatter;
Date date = new Date(dateJSON);
formatter = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = formatter.format(date);
return convertedDate;
}
Edit - Solved
public String convertDate(String dateJSON) {
String holder = dateJSON;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = format2.format(format.parse(holder));
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
Better explanation below
Answer below:
I have passed the string containing the date from JSON into this method.
If you change 'format' to match the date you're passing in (in this case it matches my dateJSON variable), and then set 'format2' to the format you're after on output :)
public String convertDate(String dateJSON) {
String holder = dateJSON;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = format2.format(format.parse(holder));
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;}

how to Change Date format in android with Proper Month?

I want to Change of date format. I am able to change the my date Format 2014-12-09 to MMM dd,yyyy(Dec 09,2014 Like this.) But It Display Jan 09,2014 instead of Dec 09,2014. How to display Dec 09,2014? Why not display proper month? Please Guide me. What is the problem in my code. please Suggest me.
My Code
SimpleDateFormat readFormat = null, writeFormat = null;
Date startDate = null, endDate = null;
String strStartDate = "2014-12-09";
readFormat = new SimpleDateFormat("yyyy-mm-dd");
writeFormat = new SimpleDateFormat("MMM dd,yyyy");
try {
startDate = readFormat.parse(strStartDate);
} catch (ParseException e) {
e.printStackTrace();
}
viewHolder.txtStartDate
.setText(writeFormat.format(startDate));
Please Guide me.
Thanks in advance.
I think you should try this:
readFormat = new SimpleDateFormat("yyyy-MM-dd"); //notice the capital M's

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.

Categories

Resources