how to Change Date format in android with Proper Month? - android

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

Related

How to paste date on desire format 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);

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.

Want to convert a date from the format"dd-mmm-yyyy" to "yyyy-mm-dd". And here is my code

I Want to convert a date from the format"dd-mmm-yyyy" to "yyyy-mm-dd". And here is my code.
public void convertDate(){
GregorianCalendar todaysDate = (GregorianCalendar) month.clone(); // Its show the error over here.
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
currentdatestring = df.format(todaysDate.getTime());
try{
Date tdate3= df.parse(currentdatestring);
tdate4=targetDateformat.format(tdate3);
}
catch (ParseException e) {
Log.e(getPackageName(), e.getMessage());
e.printStackTrace();
}
But the problem is it showing a nullpointer exception . And i am not able to understand the mistake. Please if someone can help ?
Try this :
Date cDate = new Date();
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(cDate);
Hope it will help you.
try this
DateFormat dffrom = new SimpleDateFormat("M/dd/yyyy");
DateFormat dfto = new SimpleDateFormat("yyyy-MM-dd");
Date today = dffrom.parse("7/1/2011");String s = dfto.format(today);

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.

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...

Categories

Resources