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.
我要求我需要比较两个日期。一个日期将来自DB,它是“YYYY-DD-MM”公司中的字符串,我需要将此字符串日期与当前日期进行比较。
for this I am converting Date String into Date object.
为此我将Date String转换为Date对象。
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..
现在我还需要“YYYY-MM-DD”格式的当前日期,它应该是Date对象,这样我可以使用.compareTo()方法比较两个日期..
Please help me how to do that...
请帮我怎么做......
5 个解决方案
#1
46
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
#2
5
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
.
那么你可以比较dayNow和dayDb。
#3
3
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.
如果您当前的日期实际上是java.util.Date类的实例,则无需为其指定格式;它只是一个毫秒值,代表一个特定的时刻。
You can get the current date like so:
您可以像这样获取当前日期:
Date currentDate = new Date();
#4
3
You can use 2 ways:
您可以使用以下两种方式:
-
DateFormat object. Use parse method.
DateFormat对象。使用解析方法。
-
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.
制作自己的Date解析器。我的意思是,您将年,月和日转换为每个整数,并使用Date构造函数来获取日期。
#5
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));
#1
46
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
#2
5
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
.
那么你可以比较dayNow和dayDb。
#3
3
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.
如果您当前的日期实际上是java.util.Date类的实例,则无需为其指定格式;它只是一个毫秒值,代表一个特定的时刻。
You can get the current date like so:
您可以像这样获取当前日期:
Date currentDate = new Date();
#4
3
You can use 2 ways:
您可以使用以下两种方式:
-
DateFormat object. Use parse method.
DateFormat对象。使用解析方法。
-
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.
制作自己的Date解析器。我的意思是,您将年,月和日转换为每个整数,并使用Date构造函数来获取日期。
#5
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));