Java中的日期转换
我想比较两个日期,因此我将字符串转换为日期格式。但在转换过程中,日期格式更改为“02/01/2013”和“03/01/2014”。这会使我的逻辑出错。任何人请告诉我如何比较日期格式中的两天Java中的日期转换,java,Java,我想比较两个日期,因此我将字符串转换为日期格式。但在转换过程中,日期格式更改为“02/01/2013”和“03/01/2014”。这会使我的逻辑出错。任何人请告诉我如何比较日期格式中的两天 String fdate="01/02/2012"; String tdate="01/03/2013"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date frmdt=new Dat
String fdate="01/02/2012";
String tdate="01/03/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date frmdt=new Date(fdate);
String s1 = sdf.format(frmdt);
Date todt=new Date(tdate);
String s2 = sdf.format(todt);
Date frmdate = sdf.parse(s1);
Date todate = sdf.parse(s2);
if(frmdate.compareTo(todate)<=0){
//process;
}
String fdate=“01/02/2012”;
字符串tdate=“01/03/2013”;
SimpleDataFormat sdf=新的SimpleDataFormat(“日/月/年”);
日期frmdt=新日期(fdate);
字符串s1=sdf.format(frmdt);
日期todt=新日期(tdate);
字符串s2=sdf.format(todt);
Date frmdate=sdf.parse(s1);
datetodate=sdf.parse(s2);
如果(frmdate.compareTo)(todate)请尝试以下操作:
String fs = "01/02/2012";
String ts = "01/03/2013";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
sdf.setLenient(false);
Date fdate = sdf.parse(fs);
Date tdate = sdf.parse(ts);
if (fdate.before(tdate) || f.date.equals(tdate)) {
//process;
}
你有太多事情要做。这要简单得多。试试这个:
String fs = "01/02/2012";
String ts = "01/03/2013";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
sdf.setLenient(false);
Date fdate = sdf.parse(fs);
Date tdate = sdf.parse(ts);
if (fdate.before(tdate) || f.date.equals(tdate)) {
//process;
}
你做的事情太多了。它简单多了。在我看来,你应该调用SimpleDateFormat.parse
:
// Using the US locale will force the use of the Gregorian calendar, and
// avoid any difficulties with different date separator symbols etc.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // Avoid DST complications
sdf.setLenient(false);
Date fromDate = sdf.parse(fromDateText);
Date toDate = sdf.parse(toDateText);
// Alternatively: if (!fromDate.after(toDate))
if (fromDate.compareTo(toDate) <= 0) {
...
}
//使用美国地区将强制使用公历
//使用不同的日期分隔符符号等避免任何困难。
SimpleDataFormat sdf=新的SimpleDataFormat(“dd/MM/yyyy”,Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone(“UTC”);//避免DST复杂性
sdf.setLenient(错误);
Date fromDate=sdf.parse(fromDateText);
datetodate=sdf.parse(toDateText);
//或者:如果(!fromDate.after(toDate))
如果(fromDate.compareTo)到(toDate)在我看来,您应该调用SimpleDataFormat.parse
:
// Using the US locale will force the use of the Gregorian calendar, and
// avoid any difficulties with different date separator symbols etc.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // Avoid DST complications
sdf.setLenient(false);
Date fromDate = sdf.parse(fromDateText);
Date toDate = sdf.parse(toDateText);
// Alternatively: if (!fromDate.after(toDate))
if (fromDate.compareTo(toDate) <= 0) {
...
}
//使用美国地区将强制使用公历
//使用不同的日期分隔符符号等避免任何困难。
SimpleDataFormat sdf=新的SimpleDataFormat(“dd/MM/yyyy”,Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone(“UTC”);//避免DST复杂性
sdf.setLenient(错误);
Date fromDate=sdf.parse(fromDateText);
datetodate=sdf.parse(toDateText);
//或者:如果(!fromDate.after(toDate))
如果(fromDate.compareTo(toDate)您做错了:)
String fdate=“01/02/2012”;
字符串tdate=“01/03/2013”;
SimpleDataFormat sdf=新的SimpleDataFormat(“日/月/年”);
Date frmdate=sdf.parse(fdate);
datetodate=sdf.parse(tdate);
如果(frmdate.compareTo(todate)您做错了:)
String fdate=“01/02/2012”;
字符串tdate=“01/03/2013”;
SimpleDataFormat sdf=新的SimpleDataFormat(“日/月/年”);
Date frmdate=sdf.parse(fdate);
datetodate=sdf.parse(tdate);
if(frmdate.compareTo(todate)”“它在我的逻辑中出错”根本不能解释发生了什么。为什么要使用Date(String)
构造函数而不是调用simpleDataFormat.parse
?作为旁注:Date(String)构造函数是不推荐的dusesdf.parse()
从字符串中获取日期并在比较时替换frmdate.compareTo(todate)“它在我的逻辑中出错”根本不能解释发生了什么。为什么要使用Date(String)
构造函数而不是调用SimpleDateFormat.parse
?作为旁注:Date(String)构造函数已被弃用。请使用sdf.parse()
从字符串中获取日期,并在比较它们时替换frmdate.compareTo(todate)我建议显式设置区域设置和时区,并且如果两个值相同,您的比较行为与OP的不同。@user2508515:它将在某些区域设置中工作,但在其他区域设置中不起作用,如果两个值相等,它将不会执行您希望的操作。(它不会执行if
语句的主体,而您的原始代码看起来像您希望的那样。)我建议显式设置区域设置和时区,并且如果两个值相同,您的比较行为与OP的不同。@user2508515:它将在某些区域设置中工作,但在其他区域设置中不起作用,如果两个值相等,它将不会执行您希望的操作。(它不会执行if
语句的主体,而您的原始代码看起来像您希望的那样。)实际上,将区域设置添加到日期格式是一种很好的做法。它解决了未来的许多问题;)@ChalleieMops:我不知道你为什么要添加注释-我已经添加了,并在回答中说了为什么…你只是同意我为什么这么做吗?我已经看到了你的答案,但我已经对这一点进行了评论,因为这是一个非常好的实践。真的,在日期格式中添加区域设置是一个很好的实践。它解决了很多问题在未来;)@TheCharliemops:我不知道你为什么要添加评论-我已经这样做了,并在回答中说了为什么…你只是同意我为什么这么做吗?我已经看到了你的答案,但我发表评论强调了这一点,因为这是一个非常好的做法。