Changing date format stored as a String - android

Currently I store a date in my database in the following format
dbTimestamp = new SimpleDateFormat("yyyy-MM-dd").format(myCalendar.getTime());
I am looking to pull this information in a cursor and display it to the user in the following format
String newTimestamp = new SimpleDateFormat("MM/dd/yyyy").format(date);
How would I go about converting it from 2014-07-31 to 07/31/2014?
I tried using
String transactionDate = cursor.getString(cursor.getColumnIndex("timestamp"));
String newTimestamp = new SimpleDateFormat("MM/dd/yyyy").format(transactionDate);
But i get the following error: Bad class: class java.lang.String

try {
String reformattedStr = new SimpleDateFormat("dd/MM/yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-31"));
} catch (ParseException e) {
e.printStackTrace();
}

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String get="" + dateFormat.format(date);
System.out.println("Test date is:"+get);

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

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

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

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

Date format to match the Date format in Phone Settings

I have a string which returns the date and time as 2012-11-08 12:45:30 . I need to get the date and time in separate strings and then the date has to be shown in the format which is there in the Phone's Date and Time settings.
Here is the code which I have tried so far:
date value from db is 2012-11-08
I am getting the date format in phone's settings as
String datefrmt = Settings.System.getString(
context.getContentResolver(), Settings.System.DATE_FORMAT);
the code to apply this format to the obtained date from db is:
SimpleDateFormat sdf = new SimpleDateFormat(datefrmt);
java.util.Date date_1 = sdf.parse("2012-11-08");
String s = sdf.format(date_1);
I am getting the month and day properly but the year its returning something randomly and thats not a correct value. Can anyone please guide me where am I going wrong. Thanks
If you have 2012-11-08 12:45:30 string and you want to parse it to Date object and change its format to another (system format for example or 2012-11-08 format):
try {
String yourString="2012-11-08 12:45:30";
//first we parse input date string and create Date object
String inputDateString ="yyyy-dd-MM hh:mm:ss";
SimpleDateFormat inputDateFormat = new SimpleDateFormat(inputDateString);
java.util.Date date = inputDateFormat.parse(yourString);
//log
Log.e(getClass().getName(), inputDateFormat.format(date));
//create system date format
String dateformt = Settings.System.getString( this.getContentResolver(), Settings.System.DATE_FORMAT);
//if its for some reason==null let it be "yyyy-dd-MM"
if(dateformt==null){
dateformt="yyyy-dd-MM";
}
SimpleDateFormat systemDateFormat = new SimpleDateFormat(dateformt);
String outputString = systemDateFormat.format(date);
//log
Log.e(getClass().getName(), outputString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
you can use the simpleDateFormat as:
SimpleDateFormat simpDate = new SimpleDateFormat(datefrmt,
Locale.ENGLISH);
String s = simpDate.format(new Date());
for reference, you can refer to developer site:
for more information refer this link
Since your DB date is in yyyy-MM-dd, don't use the dynamic format to get the date, otherwise it will crash(throw parse exception), instead use the static format as below to get the date. You may use the dynamic format to convert the string as below:
//use static format to convert into date from static formatted data in db
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date_1 = sdf1.parse("2012-11-08");//got the date
//use the dynamic format to convert into string
SimpleDateFormat sdf2 = new SimpleDateFormat(datefrmt);
String s = sdf2.format(date_1);
Since final String format = Settings.System.getString(getActivity().getContentResolver(), Settings.System.DATE_FORMAT);//may be NULL
So I customized my function as below for general purposes
#SuppressLint("SimpleDateFormat")
public String getCurrentShortDateFormat() {
final String[] SFs = new String [] {
"MM/dd/yyyy",
"dd/MM/yyyy",
"yyyy/MM/dd"
};
final Date date = new Date(24638400000L);//24638400000=Oct/13/1970; 0==Jan/1/1970
java.text.DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(getActivity().getApplicationContext());
String current = shortDateFormat.format(date);
SimpleDateFormat which = new SimpleDateFormat();
for (int i = 0; i < SFs.length; i++) {
which.applyPattern(SFs[i]);
if (which.format(date).compareTo(current)==0)
return SFs[i];
}
return null;
}
#Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "----- onResume.CurrentShortDateFormat-----" + getCurrentShortDateFormat());
}

Unable to change the date format in android

From a web service i am getting a date format as 1994-09-11 in my android app i need to display this as Sep 11
My code is as follows
Date date_temp = null;
String d_temp = null;
DateFormat formatter;
static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy MMM dd");
public String changeDateformat(String date_str)
{
Log.e("date_str ",""+date_str);
try
{
date_temp = (Date)formatter.parse(date_str);
Log.e("date_temp ",""+date_temp);
d_temp = null;
d_temp = datesdf.format(date_temp);
Log.e("d_temp ",""+d_temp);
}
catch (ParseException ex)
{
ex.printStackTrace();
}
catch (java.text.ParseException e)
{
e.printStackTrace();
}
return d_temp;
}
When the above method is called, the first log value is been printed(the value which i get from the web), then my app gets crashed saying that
09-10 13:02:44.920: E/AndroidRuntime(3503): java.lang.NullPointerException
09-10 13:02:44.920: E/AndroidRuntime(3503): at xxxxxxxxxxx(Events.java:206)
Here line number 206 is the first line inside try catch of the above method.
What is wrong with my code, pls suggest me.....
DateFormat formatter; <-- it is never initialized. so you are calling a methode on null.
Maybe you want to use:
static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy MMM dd");
Instead of formatter.
You can use string builder
after getting date from web
try this
//wherever you want to set this you can set as below:
enter code here
EditText aa;
aa.setText( new StringBuilder()
// Month is 0 based so add 1
.append(day).append("/")
.append(month + 1).append("/")
.append(year).append(" "));
The DateFormat is not used like that way. You declare a formatter but never initialize it. To format a date, you need to do it like this,
d_temp = DateFormat.format("MMM dd", date_temp).toString();
You need a SimpleDateFormat to convert your date string to date and another SimpleDateFormat to format your date to a new string. Try this code,
public static Date strToDate(String format, String date) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date);
} catch (ParseException e) {
return null;
}
}
public String changeDateformat(String date_str) {
Date t = strToDate("yyyy-MM-dd", date_str);
return DateFormat.format("MMM dd", t).toString();
}

Categories

Resources