Convert a string date to another date format - android

I have a date from a server in format 01/01/2012 12:00:00PM and I want to convert that date to ISO8601 format but I keep getting a Parse Exception when I try to parse the date
my code
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
Date timestamp = null;
try{
timestamp = format.parse(startDate);
startDate = format.format(timestamp);
}catch(ParseException e){
}

First parse your String and create Date object with incoming format.
DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Then create another date format with the format you are looking for and format the date object.
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
startDate = format2.format(timestamp);

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

Get date from server api and convert its format?

I am currently parsing date from PHP server api and the date from api is "1970-04-22" now I need to display this date as "04-22-1970". I tried following code but its not display the exact format.
String date = jsonObject.optString("date"); // output is "1970-04-22"
SimpleDateFormat spf=new SimpleDateFormat("MMM, dd,yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("MMM dd yyyy");
date = spf.format(newDate);
Log.e("date", String.valueOf(date));
String tempDate = "1970-04-22";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date1=null;
try {
date1 = simpleDateFormat.parse(tempDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String dateInReqFormat = newformat.format(date1);
In these cases you have to first convert the existing format to a date and then format the date to the string as you want it .

How to parse Date format server format To "MM/dd/YYYY"

I was found many solution but its not working for me.
I am trying to parse date to user selected date format.
I was getting date from server in this format 2016-06-30T00:00:00Z UTC Timezone
Now I want to Parse it in User selected format for e.g MM/dd/yyyy
How Can i do that.? I was found many solution but its not working for me.
To get date into a particular format, first you have to parse the date string into it's given format and there you will get Date object. Second, you can convert the date object into a desired format.
private String getFormattedDate(String strDate) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date d= (Date)formatter.parse(strDate);
SimpleDateFormat fmtOut = new SimpleDateFormat("MM/dd/yyyy");
return fmtOut.format(date);
}
This function help to convert any type of string date in any formate
public String getFormatedDate(String dateString, String format, String currentFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(currentFormat, Locale.ENGLISH);
try {
Date date = sdf.parse(dateString);
String formated = new SimpleDateFormat(format).format(date);
return formated;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

How to change date format coming from database to android

In my android application, I'm parsing data from mysql database using JSON and displaying in android listview. In mysql database, the date format is YYYY-MM-DD and I am getting this format in android using JSON as a String. Is it possible to change date format to DD-MM-YYYY in android instead of database..??
Use SimpleDateFormat to convert date form one format to another format :
SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY");
try{
Date d = df1.parse(DateString);
System.out.println("new date format " + df2.format(d));
}catch(Exception e){
e.printStackTrace();
}
Yes, it is. Use SimpleDateFormat :
String inputPattern = "yyyy-MM-dd";
String outputPattern = "dd-MM-yyyy";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String result = null;
try {
date = inputFormat.parse(time);
result = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//use the result variable as you wish
this will be the answer i need :D
String well=String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
java.util.Locale.getDefault()).format(new java.util.Date()));

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