This question already has answers here:
Date formatting based on user locale on android
(8 answers)
Closed 7 years ago.
In my Android project, I have 3 variables (string) :
String day = "26";
String month = "03";
String year = "1989";
I would like to have a variable (String date) with the date format of the device (depending of the langage) like this :
If the device is in french, date = "26/03/1989"
If the device is in english (USA), date = "03/26/1989"
etc
How can I do that ?
Thanks
Join your strings into one like in this example (obviously you can omit the date)
String dateString = "03/26/2012 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Prints:
Mon Mar 26 11:49:00 EEST 2012
EDIT: This example doesn't show how to get the phone local date format
SimpleDateFormat already support that.
You can especify your desired Locale or get the default from the device.
Take a look at the docs.
Hope this helps.
Related
This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 4 years ago.
I'm using a scanner that gets details from a UK driving licence. However it returns the date as a string and most current uk driving licences are dd-mm-yy (16-10-10). Is there a way I can make it dd-mm-yyyy but with the right two numbers in front of it i.e. 16-10-2010?
Thanks
String strDate = "16-10-10";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
try {
Date date = dateFormat.parse(strDate);
Format formatter = new SimpleDateFormat("MM-dd-yyyy");
String s = formatter.format(date);
System.out.println(s);
} catch (ParseException e) {
e.printStackTrace();
}
This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Android SimpleDateFormat, how to use it?
(11 answers)
Closed 5 years ago.
I am trying to compare two dates in android in the 24 hour format following is the date formats but both are giving same result when trying one after other.
SimpleDateFormat simpleDateFormat24Hour = new SimpleDateFormat("mm/dd/yyyy HH:mm");
SimpleDateFormat simpleDateFormat24Hour = new SimpleDateFormat("mm/dd/yyyy HH:mm", Locale.US);
Log.e(startDateEditText.getText().toString().trim());
Log.e(endDateEditText.getText().toString().trim());
Date startDate = simpleDateFormat24Hour.parse(startDateEditText.getText().toString().trim());
Date endDate = simpleDateFormat24Hour.parse(endDateEditText.getText().toString().trim());
if (startDate.compareTo(endDate) > 0) {
showAlertDialog("End date should greater than start date.");
}
following are the inputs for start and end date respectively
04/20/2017 11:07
05/18/2017 11:22
if the end date selection is one month greater or if select next month's date then this issue arises but if date selection is from same month then this works fine.
please suggest some tips to resolve this issue. Thanks in advance.
Try this,
if (false == isDateAfter("04/20/2017 11:07", "05/18/2017 11:22")) {
showAlertDialog("End date should greater than start date.");
}
isDateAfter Method:
public static boolean isDateAfter(String startDate, String endDate) {
try {
String myFormatString = "dd-MM-yyyy"; // for example
SimpleDateFormat df = new SimpleDateFormat(myFormatString, Locale.ENGLISH);
Date endingDate = df.parse(endDate);
Date startingDate = df.parse(startDate);
return endingDate.equals(startingDate) || !endingDate.after(startingDate);
} catch (Exception e) {
return false;
}
}
User will input a date and he/she will input days to add to it.
Like this:
Date: 1/1/2015
Days to add: 20
The output should be 1/21/2015
I am wondering how to do that. I am beginner in android. T__T cries I tried other sources but i don't understand them at all. THANKS
THE USER WILL SUPPLY THE DATE TO ADD AND THE NUMBER OF DAYS TO ADD. All other sources only explain adding the current date with the number of days.
You need to parse the string to a date first.
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy");
try {
Date myDate = dateParser.parse("01/01/2015");
Calendar c = Calendar.getInstance();
c.setTime(myDate);
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 20);
Date newDate = c.getTime();
String newFormattedDate = dateParser.format(newDate);//01/21/2015
} catch (ParseException e) {
e.printStackTrace();
//handle exception
}
I am building an app that will required the dates to be formatted to the locale. I have it working using the below code, however Sweden use the date format yyyy-mm-dd but it is giving me dd.mm.yyyy. I have looked into localizedpattern but struggling to find a decent example of how this is implemented or if its any different from what I am already trying to do.
public String formatDate(String dateToFormat){
Date date=null;
Configuration sysConfig = getResources().getConfiguration();
Locale curLocale = sysConfig.locale;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
date = sdf.parse(dateToFormat);
} catch (ParseException e) {
//
}
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,curLocale);
return dateFormat.format(date);
}
The dateformat from the Netherlands is ("dd/MM/yyyy") with 4 yyyy, not 3 yyy.
This question already has answers here:
Convert UTC to current locale time
(7 answers)
Closed 9 years ago.
I have a UTC time in this format 2013-03-04T14:37:15 How can I convert this into local time and then to one minute ago format.
I tried this first to convert the time to local format:
private String getDate(String date) {
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
fDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fDate.toString();
}
But the above functions throws an exception saying unparseable date..
The parse exception you get is because of the extra T in your date(2013-03-04T14:37:15).
You can either remove that extra 'T' and parse the date with your current date format,
Or if you need to parse it with the T, change your format to parse the literal T as well,
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Check DateUtils.getRelativeTimeSpanString() (one methods with the same name from DateUtils). It will be something like :
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
fDate = simpleDateFormat.parse(date);
return DateUtils.getRelativeTimeSpanString(context, System.currentTimeMillis()-fDate.getTime())'