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();
}
Related
This question already has answers here:
how to convert long date value to mm/dd/yyyy format [duplicate]
(4 answers)
Closed 5 years ago.
I am working on some error like i am getting response of a date which is like 1510808400000 and i have to display this as a valid date format like dd-mm-yy Suggest me is there any way to convert that long value to valid date format. or guide me possible changes.
modify date format as required.
private String getDate(long timeStamp){
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM dd, yyyy");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
}
catch(Exception ex){
ex.printStackTrace();
return null;
}
}
Use below code
long millis = Long.parse("1510808400000 ");
Date date = new Date(millis);
String displayDate = new SimpleDateFormat("dd-mm-yy").format(date);
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;
}
}
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.
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
Mon, 24 Feb 2014 05:18:24 +0000
I want to convert above string to datetimeformat and save to sqlite table. Please help me.
use below Code:
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After getting string store in sqlite data base.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
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())'