How to Compare system date with mydate in android 2.1? - android

in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.

You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.

Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());

Related

Android:Display time after adding GMT time zone

I am working on a App in which i want to display notification time.
I can display notification time but not able to add time zone in it.
My current location is Pakistan and i want to add GMT+5:00
My code is attached
String currentDateTimeString = DateFormat.getTimeInstance().format(notif.At);
textViewTime.setText(currentDateTimeString);
in this code, notif.At is dateTime variable. I also attached screenshot of my app, i want to ask you , how to add timeZone value in notif.At. Thanks!
Update
You mark time with timezone in order to solve internationalization problem, I understand, right?
If so, I think it could be better to convert your date to UTC date. When you change to another timezone, just convert this UTC Date to local.
public static Date localToUtc(Date localDate) {
return new Date(localDate.getTime()-TimeZone.getDefault().getOffset(localDate.getTime()));
}
public static Date utcToLocal(Date utcDate) {
return new Date(utcDate.getTime()+TimeZone.getDefault().getOffset(utcDate.getTime()));
}
Old answer
If your notif.At is Dateobject, it's a same question actually:
TimeZone tz = TimeZone.getDefault();
Date date = new Date();
final String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String result = sdf.format(date);
Log.d("Date ", "date: " + result + " " + tz.getDisplayName(false, TimeZone.SHORT));
print:
date: 2015-03-31 18:45:28 GMT+08:00
You can try java.time api;
Instant date = Instant.ofEpochMilli(1549362600000l);
LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
LocalDateTime pst = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:00"));
LocalDateTime is = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:30"));

Format date for SQLite and subtract minutes

I need to subtract an X number of minutes from the current date and format it for a SQLlite query in my Android app. This is what I have so far:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -60); //one hour back
final SimpleDateFormat std = new SimpleDateFormat("yyyy-mm-dd hh:mm");
std.setCalendar(cal);
String date = std.format(cal.getTime());
String sql = "SELECT * FROM [tbl_name] WHERE [datefield] >= " + date;
datefield is stored as a DATETIME in the SQLlite table. I don't want to use Joda time because I want to keep the number of dependencies in my app to a minimum.
With my current code, the date date variable is coming out as: '2012-43-05 07:43'
You are using minutes twice in your format. mm actually needs to be MM in the date portion.
final SimpleDateFormat std = new SimpleDateFormat("yyyy-MM-dd hh:mm");

How to get Date object in “YYYY-MM-DD” format in android

I have a requirement that I need to compare two Dates. One Date will come from DB which is String in "YYYY-DD-MM" firm and I need to compare this String Date with current Date.
for this I am converting Date String into Date object.
Now I need current Date also in "YYYY-MM-DD" format and it should be Date object so that I can use.compareTo() method compare two dates..
Please help me how to do that...
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
You can do it in following way
// pick current system date
Date dt = new Date();
// set format for date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// parse it like
String check = dateFormat.format(dt);
System.out.println("DATE TO FROM DATEBASE " +
arrayOfStringDate[d].toString());
System.out.println("CURRENT DATE " + check);
// and compare like
System.out.println("compare "+
arrayOfStringDate[d].toString().equals(check));
Calendar c = Calendar.getInstance();
SimpleDateFormat tf = new SimpleDateFormat("yyyy-MM-dd");
String time=DB time;
Date parseTime= tf.parse(time);
Integer dayNow=c.getTime().getDate();
Integer dayDb=parseTime.getDate();
then you can compare dayNow and dayDb.
If your current date is actually an instance of the java.util.Date class, you don't need to specify a format for it; it's just a millisecond value that represents a specific moment in time.
You can get the current date like so:
Date currentDate = new Date();
You can use 2 ways:
DateFormat object. Use parse method.
Make your own parser of the Date. I mean, you convert the year, month and day in an integer each, and use Date constructor to get the Date.

displaying date in edit text android

In my application i have edit text that will show the current date and time initially . If i change the date to some other date and when i tried to use gettext() i am still getting the current date. can anyone help. This is my code to set initial value in edit text
TaskTime = (EditText) findViewById(R.id.txtTaskTime);
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
TaskDate.setText(dateFormat.format(date));
Most of the use of date object is obsolete in Android, you should use Calendar : http://developer.android.com/reference/java/util/Calendar.html
Yes, you can get the current date because , Date date = new Date(); this function always returns the current date .
So you need to use the Calendar object to get the current date and time .
Example
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String strDate = sdf.format(cal.getTime());
Here in dateFormat you have to declare your date format . dateFormat = "dd/MM/yyyy" like .

Android load timezone too long: Loaded time zone names for en_US

Dear all, i just code a snippet code to get date time string as below:
public static String getCurrentDate(){
Locale.setDefault(Locale.US);
Date date = new Date();
String strDate = date.toString();
return strDate;
}
But problem is it take too long time (about 2 seconds) to convert from Date to string, Logs:
10-11 17:52:51.733: INFO/Resources(6835): Loaded time zone names for en_US in 2107ms.
Could you please give me a solution how to increase performance of this method
Update for solution:
I just found an solution by tronman at topic:
How do you format date and time in Android?
As below:
Date date = new Date();
java.text.DateFormat dateFormat =
android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
Yet another solution is to use SimpleDateFormat with default locale
new SimpleDateFormat("dd/MM", Locale.getDefault());

Categories

Resources