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()));
Related
My date string looks like this "2022-02-04T12:20:47,398000+01".
I tried this format SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSSSSZ") but it throws an exception.
Minimum API level that I need to support is 23, so I can't use 'X' symbols in the format like "yyyy-MM-dd'T'HH:mm:ss,SSSSSSX".
Is there a safe way to parse the date string I have on android?
use the following
String inputDate = "2022-02-04T12:20:47,398000+01";
String result;
SimpleDateFormat inputformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date convertDate = inputformat.parse(inputDate);
result = outputformat.format(convertDate);
tvResult.setText(result);
}catch (Exception e){
e.printStackTrace();
}
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 convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android .
The actual value I get from server is in yr-mnth-day format. But I need to display it as day-mnth-yr in my app.How can I achieve this?
First you'll need to parse the String into a Date. Then you format the date according to your needs, the example below is an edited version of BalusC's answer from this topic: Change date format in a Java string
String oldstring = "2015-05-12";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
//Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("dd-MM-yyyy").format(date);
System.out.println(newstring); // 12-05-2015
If you say this, i suppose you mean in a string, so you can convert string to Date with
String dateServer = "2015-05-12";
//Identify the format which has been used from server
DateFormat format = new SimpleDateFormat("yyyy, MM, dd", Locale.ENGLISH);
//Convert string to Date using this format
Date date = format.parse(string);
//Create the format you need to print
DateFormat wellFormatted = new SimpleDateFormat("dd/MM/yyyy");
//now convert the date into a string formatted as you like
String wellPrintedDate = wellFormatted.format(date);
that's all, "wellPrintedDate" is the string in format day/month/yeah. here you can find the list of code used for formatting :)
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date past = format.parse("2015-05-12");
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("dt=" + format1.format(past));
} catch (ParseException e) {
e.printStackTrace();
}
public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
inputFormat = "yyyy-MM-dd";
}
if(outputFormat.equals("")){
outputFormat = "dd-mm-yyyy"; // if inputFormat = "", set a default output format.
}
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
// You can set a different Locale, This example set a locale of Country Mexico.
//SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
//SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (Exception e) {
Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
}
return outputDate;
}
call like this
Log.e("",""+formattedDateFromString("yyyy-MM-dd","dd-MM-yyyy","2015-05-12"));
output: 12-05-2015
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.
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...