How to get Date object in “YYYY-MM-DD” format in android
Solution 1
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
Solution 2
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
.
Solution 3
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));
Solution 4
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();
Solution 5
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.
brig
Updated on November 12, 2020Comments
-
brig over 3 years
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...