Compare Date String in Android - android

I have two strings, the first one contains an actual date, and the second one contains a date format.
I want to compare both the strings. Here is my code:
for current date
Date referenceDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(referenceDate);
c.add(Calendar.MINUTE, -30);
//c.getTime();
//System.out.println("Date class"+c.getTime());
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateandTime = sdf.format(c.getTime());
System.out.println("Simple Date Format "+currentDateandTime);
and 2nd date code is here
private void setDate() {
try {
sbMain = obj.get("date_of_report").toString().split(" ");
} catch (JSONException e) {
}
String[] sb = sbMain[0].split("-");
String[] sb2 = sbMain[1].split(":");
System.out.println("Alert Time : " + sb[0] + " " + sb[1] + " " + sb[2] + " " + sb2[0] + ":" + sb2[1] + ":" + sb2[2]);
}

When you have all the components of your second date, e.g Day,time,Month and Year then use the same Calendar class to obtain date object by setting correct time and date components you retrieved, now you have two valid instances of your dates, get the values of both in Millis, and perform whatever comparision you want.

Related

How to parse T date format in Android?

I am working on date format application, i have one T date format but i am not able to parse this format.
try {
String dtStart = "2016-07-20T11:51:31.744Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
if (dtStart != null) {
Date date = format.parse(dtStart);
Calendar myCal = new GregorianCalendar();
myCal.setTime(date);
String promoPendingDay = "" + myCal.get(Calendar.DAY_OF_MONTH);
int promoPendingMonth = myCal.get(Calendar.MONTH) + 1;
String promoPendingYear = "" + myCal.get(Calendar.YEAR);
mMessageStartDateLabel.setText(promoPendingDay + "/" + promoPendingMonth + "/" + promoPendingYear);
}
} catch (ParseException e) {
e.printStackTrace();
}
every time i am getting one exception java.text.ParseException: Unparseable date:
Please kindly go through my code and suggest me some solution.
Here a solution applying two changes. One change is to add the fractional seconds, another change is to set the UTC-timezone.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT")); // very important, input Z means UTC+00

Convert Received date time to local timezone time Android

Good day.I am building an chat application.For purpose i decided to put a date of message had been sent.No wounder that in different countries the date must show different.For example let's take 2 different countries and assume the difference between them are 2 hours.CountryX and CountryY.The user send message from CountryX which time is lets say 15:00.I am saving this on server with exact timezone time as user sent,exactly 15:00 as CountryX.Second user receives the message in CountryY which is more in 2 hours from CountryX,so basically The time which I MUST show in CountryY must be 17:00.This is the issue.How can i convert an already received time with known timezone to local in order to show correct?I did google a lot but all i came up,were solutions where you would simply just get an time for exact country,and not convert the CountryX sent Time to CountryY local time to show it correctly in CountryY.Please can you provide an help?Thank you very much beforehand.
For everyone suffering because of this.I have ended up creating my own class with my own logic and it works flawlessly and as an extra bonus couple of handy methods with time
public class Time {
public static String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String finalFormat = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
if (month < 10) {
String finalMonth = "0" + month;
finalFormat = year + "-" + finalMonth + "-" + day + " " + hour + ":" + minute + ":" + second;
}
return finalFormat;
}
public static String convertToLocalTime(String timeToConvert) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone(Constants.SERVER_TIME_ZONE));//where server time zone is the time zone of your server as defauul,E.X -America/Los_Angeles
Date parsed;
try {
parsed = sourceFormat.parse(timeToConvert);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
return result;
}
public static String getTimeZone() {
return TimeZone.getDefault().getID();
}
}

How to set 24h in Calendar method on Android?

I want develop TODO-List application with Realm Database, this application user can set Title, Date and time for Task/Todo .
I want when user set Date, hour save to database with 24h instead of am/pm.
for example : i want show hour this formatting 17:34 , but show me 05:34 !
I use this codes, but show me am/pm formatting.
Calendar method and save to database :
// Calendar Method
Calendar calender = Calendar.getInstance();
calender.set(Calendar.DAY_OF_MONTH, dialog_date.getDay()-1);
calender.set(Calendar.MONTH, dialog_date.getMonth()-1);
calender.set(Calendar.YEAR, dialog_date.getYear());
calender.set(Calendar.HOUR, dialog_date.getHour());
calender.set(Calendar.MINUTE, dialog_date.getMinute());
long now = System.currentTimeMillis();
Realm realm = Realm.getDefaultInstance();
Task_Provider task_provider = new Task_Provider(addTask, now, calender.getTimeInMillis(), false);
Code 1 :
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy" + "\n" + " " + "hh:mm");
public void setDate(long when_date) {
card_date.setText(dateFormat.format(new Date(when_date)));
}
}
Code 2 :
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy" + "\n" + " " + "kk:mm");
public void setDate(long when_date) {
card_date.setText(dateFormat.format(new Date(when_date)));
}
}
Code 3 :
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy" + "\n" + " " + "HH:mm");
public void setDate(long when_date) {
card_date.setText(dateFormat.format(new Date(when_date)));
}
}
but this code not work for me!
How can save 24h in calendar method on Android? tnx all <3

Trying to change calendar date format

I'm trying to convert the calendar which i set the time milliseconds from 1 1 1970 as below:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(1417780800);
Although when I'm trying to convert this calendar to a string as in this format "yyyy-MM-dd HH:mm:ss" it always set the date as 1970-1-17 9:49:40 when it should be 2014-12-1 12:00:00
I used this way to convert the date:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(c.getTime());
Also, using this way is getting wrong:
String date = "" + c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
Any idea why?
Thanks in advance.
You can try something like...
DateFormat.getDateInstance().format(1417780800);
It will return Date in String format.
Jan 17, 1970

Converting Timestamp as String to Date in android

I am getting Timestamp value as 1291204449 from server so I extracted the value by
implementing handler.
Now I want to convert timestamp value to date.(But the problem is in my activity I stored this value in a string variable because handler returning in string format).
Just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
I have a function for converting timestamps in String Format:
public static String create_datestring(String timestring){
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(timestring));
timestring = add_string(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)),"0",2,0) + "." + add_string(String.valueOf(cal.get(Calendar.MONTH)+1),"0",2,0) + "." + String.valueOf(cal.get(Calendar.YEAR)) + " " + add_string(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)),"0",2,0) + ":" + add_string(String.valueOf(cal.get(Calendar.MINUTE)),"0",2,0);
return timestring;
}

Categories

Resources