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());
}
Related
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();
}
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 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
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);
I am debugging a android framework.
I pull out dropbox logs from device and it's created in /data/system/dropbox.
Log file name is printed like this format.
event_data#1362451303699
1362451303699 is timestamp and i want to change it like 05/03/2013 16:00 for legibility.
How can i convert this timestamp?
Is there any code needs to be changed?
Any help will be much appreciated.
use: Date date = new Date(timestamp);
Edit full code:
String wantedDate = "";
String log = "event_data#1362451303699";
int index = log.indexOf("#");
if(index != -1) {
index = index + 1; // skip # symbol
if(index < log.length()) { // avoid out of bounds
String logtime = log.substring(+1);
try {
long timestamp = Long.parseLong(logtime);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = new Date(timestamp);
wantedDate = df.format(date);
} catch (NumberFormatException nfe) {
// not a number
}
}
}
if( ! "".equals(wantedDate) ) {
// everything OK
} else {
// error cannot retrieve date!
}
Related doc:
indexOf : http://developer.android.com/reference/java/lang/String.html#indexOf%28java.lang.String%29
SimpleDateFormat : http://developer.android.com/reference/java/text/SimpleDateFormat.html
you can use a SimepleDateFormat to parse it. For example:
long ts = 1362451303699;
Date date = new Date(ts);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(date));
With the SimpleDateFormat you can bring your Date in a more readable format.
It is a UNIX epoch timestamp, all you need to do is to convert the String representation of the number to long, then you can use it to create a Date object, which you can format with DateFormat. Something like this:
// Get this from the log
String timestamp = "1362451303699";
long epoch = Long.parseLong(timestamp);
Date date = new Date(epoch);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = format.format(date);