Reputation: 3773
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...
Upvotes: 22
Views: 61606
Reputation: 1
You can call SimpleDateFormat , and from simpledateformat you will access to current date in string forme.
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String currentTime=simpleDateFormat.format(new Date());
Log.d("Currentdate",currentdate);
Upvotes: 0
Reputation: 4433
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));
Upvotes: 6
Reputation: 3174
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
.
Upvotes: 5
Reputation: 4356
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
Upvotes: 65
Reputation: 1135
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.
Upvotes: 3
Reputation: 38345
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();
Upvotes: 4