Exception on Date parsing android [duplicate] - android

This question already has answers here:
java.text.ParseException: Unparseable date
(3 answers)
Closed 5 years ago.
I am using a function to take out the difference between a given date and current date. At all the places, that function is working just fine but at one place, it is throwing the unparseable date exception.
Below is the function that I use:
public boolean timeDiff24hrs(String alarmTime) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date();
String s = sdf.format(date);
String alarmDateStr="";
try {
Date alarmDate=sdf.parse(alarmTime);
alarmDateStr=sdf.format(alarmDate);
} catch (ParseException e) {
e.printStackTrace();
}
int diff = diffOf2Days(alarmDateStr, s);
if (diff > 1439) {
return true;
}
return false;
}
Here I get the exception trying to execute the try block.
The alarmDate that I pass into the function is :
1/31/2018 8:58:12 AM +00:00
Can someone please explain.

Your date format MM/dd/yyyy'T'HH:mm:ss which your using is incorrect 1/31/2018 8:58:12 AM +00:00 and return different value from expected.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
return 1/31/2018T8:58:12
And you are trying to pass below
1/31/2018 8:58:12 AM +00:00

Related

Covnert string of dd-mm-yy to dd-mm-yyyy [duplicate]

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

can i change the date 1510808400000 to a valid date format in android [duplicate]

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

Issue in date comparision using compareTo() method in android [duplicate]

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

How to not convert Date and time to any time zone

I am accessing Dot net web services and using Ksoap library as my web services are Soap based.
Basically i want to save the dates and show them in list at it is. I do not want the date conversion to any specific region or time zone.
my dates which are coming from services has following pattern please have an example of threee different fields.
patient DOB = 1974-05-18T00:00:00
Collection Date = 2016-07-27T11:00:00
attachment Date uploaded = 2016-09-28T10:19:23.48
and I am using following method to convert them
public static Date convertStringToDate(String date,String dateFormat)throws Exception {
Date output = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
try {
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
output = simpleDateFormat.parse(date);
} catch (ParseException e) {
throw e;
}
return output;
}
where dateFormat is "yyyy-MM-dd" and whereas date could be any string shown above in above example.
Problems:
1> When I convert date using that method I sometimes get accurate time and date
2> Some times I observed slightly changed in time , like 2 to 7 hours shift in time. this is due to time zone conversion
3> Some times I observe a whol day shift. Let suppose if the date that was coming from server was 2016-09-28T10:19:23.48 after conversion it becomes 2016-09-27 to me .
Whats wrong ? How can I simple show date as it is from web services How can i get that except saving dates directly in strings and showing those strings by splitting.
Please help me.
Update
i am converting back my date object back to string to show on UI in following manner
public static String convertDateToString(Date date,String dateFormat)throws Exception {
String output = "";
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
output = simpleDateFormat.format(date);
} catch (Exception e) {
throw e;
}
return output;
}
To achieve that, you need to know the actual timezone and dateformat of the server. If you knew it already, the following snippet would be useful
public static String parseDateFormat(String incomingDate, String inputFormat, String outputFormat) {
String finalDate = null;
SimpleDateFormat input = new SimpleDateFormat(inputFormat); // server date format
input.setTimeZone(TimeZone.getTimeZone(SERVER_TIMEZONE)); // timezone set in the server
SimpleDateFormat output = new SimpleDateFormat(outputFormat); // format to which you want to convert
output.setTimeZone(Constants.UTC_TIMEZONE); // timezone to which you want to convert
try {
Date realDate = input.parse(incomingDate);
finalDate = output.format(realDate);
} catch (ParseException e) {
Log.e("Error", e.getMessage(),e);
}
return finalDate;
}
Setting the time zone on the date format adds an offset to the time stamp. And your server's time format doesn't include any time zone.
Remove the line simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
You generally need to set the time zone only when you intend to account for the difference between locales.

Covert UTC time format to local time [duplicate]

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

Categories

Resources