Java 将日历日期转换为字符串?
我必须比较两个日期,第一个日期是日历格式,另一个是字符串(DD-MMM-yyyy)格式。所以我想将其中一个日历日期转换为字符串,并使用compareTo方法 我试过使用:Java 将日历日期转换为字符串?,java,android,date,calendar,Java,Android,Date,Calendar,我必须比较两个日期,第一个日期是日历格式,另一个是字符串(DD-MMM-yyyy)格式。所以我想将其中一个日历日期转换为字符串,并使用compareTo方法 我试过使用: SimpleDateFormat formatter=new SimpleDateFormat("DD-MMM-yyyy"); String currentDate=formatter.format(view.getSelectedDay()); 假设view.getSelectedDay()返回一个日历,您可能
SimpleDateFormat formatter=new SimpleDateFormat("DD-MMM-yyyy");
String currentDate=formatter.format(view.getSelectedDay());
假设
view.getSelectedDay()
返回一个日历
,您可能只需要:
String currentDate = formatter.format(view.getSelectedDay().getTime());
(这样您就有了一个日期
引用以传递到格式
)
如果这不是问题所在,请提供更多信息。顺便说一句,我想你也想要“dd”而不是“dd”。根据文档,“DD”是一年中的一天,“DD”是一个月中的一天。您的问题已经解决。无论如何,为什么要通过字符串表示来比较日期呢?比较像这样的日期对象不是更好吗?您可以使用Calendar类的getTime()方法获取Date对象。您是在比较这些值以确定一个日期是在另一个日期之前,还是在排序?如果你是这样的话,你可能会因为字典排序而与某种排序顺序发生冲突
String s = "12-11-2001";
String s2 = "13-11-2000";
int i = s.compareTo(s2);
System.out.println(i);
它的输出是-1,其中应该是1,因为s2作为一个日期早于s,但s2在按升序排序时按字典顺序在s之后
您可能会发现将字符串日期转换为日期对象更为明智,然后使用before()或after()。我认为最好是比较
date
对象或Calendar
对象。非常分解,它给出:
- 比较
对象日期
final Calendar calendarDate = your_date_as_a_Calendar; final String stringDate = your_date_as_a_String; final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy"); final Date dateA = calendarDate.getTime(); // this gives the absolute time, that actually embeds the date! final Date dateB = format.parse(stringDate); final int comparison = dateA.compareTo(dateB);
final Calendar calendarA = your_date_as_a_Calendar; final String stringDate = your_date_as_a_String; final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy"); final Calendar calendarB = new GregorianCalendar(); calendarB.setTime(format.parse(stringDate)); final int comparison = calendarA.compareTo(calendarB);
- 比较
对象日历
final Calendar calendarDate = your_date_as_a_Calendar; final String stringDate = your_date_as_a_String; final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy"); final Date dateA = calendarDate.getTime(); // this gives the absolute time, that actually embeds the date! final Date dateB = format.parse(stringDate); final int comparison = dateA.compareTo(dateB);
final Calendar calendarA = your_date_as_a_Calendar; final String stringDate = your_date_as_a_String; final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy"); final Calendar calendarB = new GregorianCalendar(); calendarB.setTime(format.parse(stringDate)); final int comparison = calendarA.compareTo(calendarB);
A
,>0
如果A>B
,>0
如果A>B
,==0
,根据或
唯一需要注意的是:
- 一天中的时间:如果您的
赌注设置为与您的日历
相同的日期,但如果时间不同,则不起作用(我们在这里比较瞬间)字符串
的模式:它应该与simpleDataFormat
字符串的格式匹配,否则会产生奇怪的结果
- 区域设置:您的日期可能指的是同一个瞬间,但如果在不同的时区表示,则可能不同!如果您必须处理此问题,则在使用
日历时,您必须注意
(有关详细信息,请参阅和的文档)时区
SimpleDataFormat(“yyyy-MM-dd”)
。使用SimpleDateFormat(“dd-MM-yyyy”)
格式进行比较在大多数情况下都是错误的,因为首先检查的是最低有效数字,最后检查的是最高有效数字。如果必须使用
dd-MM-yyyy
格式,则可以编写一个函数,拆分字符串,然后按正确顺序比较年/月/日,并返回正、负或零
// Compares first date to second date and returns an integer
// can be used in a similar manner as String.CompareTo()
Public Static int CompareDates(String Date1, String Date2) {
String[] splitDate1 = Date1.split("-");
String[] splitDate2 = Date2.split("-");
int ret = -1;
if (splitDate1[2].CompareTo(splitDate2[2]) == 0) {
if (spliDatet1[1].CompareTo(splitDate2[1]) == 0) {
if (splitDate1[0].CompareTo(splitDate2[0]) == 0) {
ret = 0;
}
else if (splitDate1[0].CompareTo(splitDate2[0]) > 0) {
ret = 1;
}
}
else if (splitDate1[1].CompareTo(splitDate2[1]) > 0) {
ret = 1;
}
}
else if (splitDate1[2].CompareTo(splitDate2[2]) > 0) {
ret = 1;
}
Return ret;
}