如何比较日历';到java.sql.time对象的时间是多少?
我想知道日历对象的时间值是否等于java.sql.Time对象的值 例如 我试过了如何比较日历';到java.sql.time对象的时间是多少?,java,datetime,date,calendar,Java,Datetime,Date,Calendar,我想知道日历对象的时间值是否等于java.sql.Time对象的值 例如 我试过了 t.equals(c.getTime()) 但由于日历包含日期信息,因此表达式为false 比较两者的最佳方式是什么 编辑: 时间对象是通过Hibernate检索的,没有日期信息 日历对象由创建 Calendar c= Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 9); c.set(Calendar.MINUTE, 0); c.set(Calendar
t.equals(c.getTime())
但由于日历包含日期信息,因此表达式为false
比较两者的最佳方式是什么
编辑:时间对象是通过Hibernate检索的,没有日期信息 日历对象由创建
Calendar c= Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
你用的方法很好。不过,目标还不清楚。为什么希望
c
等于d
此外,没有办法让d.toString()=“09:00:00”
-日期始终包含日期
不过,更重要的是,Date
没有时区信息(它以前有,但不鼓励您触摸Date
)的这一部分),因此您无法从10:00BST
中分辨09:00UTC
——也就是说,除非您指定了时区。您可以从日历c
中获取时区,它在某种程度上解释了您需要执行的操作:
从您的日期创建日历
从已使用的日历复制时区
比较您感兴趣的日历
字段。我想那将是小时,分钟,秒,也许还有毫秒
更新:既然您已经提到它实际上是java.sql.Time
,我很担心。问题是,
- SQL Server通常将时间存储为包含小时、分钟、秒等的结构。也就是说,有一个隐含的时区(SQL Server时区)
java.sql.Time
将自1970年1月1日的“零历元”值以来的时间存储为毫秒。日期部分通常被剥离到1970年1月1日,但此类不包含时区信息。(好吧,再说一次,有点像,但它已经被弃用了。)
日历
有一个明确设置的时区
实际上,它的意思是,从服务器读取时间,然后读取该值,并将其与具有自己时区的日历进行比较
如果它听起来令人困惑和脆弱,那是因为它是。所以基本上你有三个时区:
SQL Server TZ
JVM的默认TZ
历法
这三者必须相同,以便任何比较都有意义 你的使用方法非常好。不过,目标还不清楚。为什么希望c
等于d
此外,没有办法让d.toString()=“09:00:00”
-日期始终包含日期
不过,更重要的是,Date
没有时区信息(它以前有,但不鼓励您触摸Date
)的这一部分),因此您无法从10:00BST
中分辨09:00UTC
——也就是说,除非您指定了时区。您可以从日历c
中获取时区,它在某种程度上解释了您需要执行的操作:
从您的日期创建日历
从已使用的日历复制时区
比较您感兴趣的日历
字段。我想那将是小时,分钟,秒,也许还有毫秒
更新:既然您已经提到它实际上是java.sql.Time
,我很担心。问题是,
- SQL Server通常将时间存储为包含小时、分钟、秒等的结构。也就是说,有一个隐含的时区(SQL Server时区)
java.sql.Time
将自1970年1月1日的“零历元”值以来的时间存储为毫秒。日期部分通常被剥离到1970年1月1日,但此类不包含时区信息。(好吧,再说一次,有点像,但它已经被弃用了。)
日历
有一个明确设置的时区
实际上,它的意思是,从服务器读取时间,然后读取该值,并将其与具有自己时区的日历进行比较
如果它听起来令人困惑和脆弱,那是因为它是。所以基本上你有三个时区:
SQL Server TZ
JVM的默认TZ
历法
这三者必须相同,以便任何比较都有意义 您可以使用、、
等类来处理Java中的日期时间。让我们看一些例子
SimpleDateFormat dayFormat = new SimpleDateFormat("D");
int _currentDay = Integer.parseInt(dayFormat.format(new Date(System.currentTimeMillis())));
SimpleDateFormat monthFormat = new SimpleDateFormat("M");
int _currentMonth = Integer.parseInt(monthFormat.format(new Date(System.currentTimeMillis())));
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
int _currentYear = Integer.parseInt(yearFormat.format(new Date(System.currentTimeMillis())));
System.out.println(_currentDay+"/"+_currentMonth+"/"+_currentYear);
将基于当前毫秒显示当前日期
用户的变体:
String toDateAsString = "07/01/2012";
DateTime toDate = DateTimeFormat.forPattern("MM/d/yyyy").parseDateTime(toDateAsString);
DateTime now = new DateTime();
if (!toDate.toLocalDate().isBefore(now.toLocalDate()))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
您可以使用、、
等类来处理Java中的日期时间。让我们看一些例子
SimpleDateFormat dayFormat = new SimpleDateFormat("D");
int _currentDay = Integer.parseInt(dayFormat.format(new Date(System.currentTimeMillis())));
SimpleDateFormat monthFormat = new SimpleDateFormat("M");
int _currentMonth = Integer.parseInt(monthFormat.format(new Date(System.currentTimeMillis())));
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
int _currentYear = Integer.parseInt(yearFormat.format(new Date(System.currentTimeMillis())));
System.out.println(_currentDay+"/"+_currentMonth+"/"+_currentYear);
将基于当前毫秒显示当前日期
用户的变体:
String toDateAsString = "07/01/2012";
DateTime toDate = DateTimeFormat.forPattern("MM/d/yyyy").parseDateTime(toDateAsString);
DateTime now = new DateTime();
if (!toDate.toLocalDate().isBefore(now.toLocalDate()))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
你为什么不以毫秒为单位来比较时间呢
Date d;
Calendar c;
System.out.println(d.getTime() == c.getTimeInMillis());
你为什么不以毫秒为单位来比较时间呢
Date d;
Calendar c;
System.out.println(d.getTime() == c.getTimeInMillis());
既然你用DateTime标记了这个问题,我想你已经用Joda了
...
//Initialize Calendar and Date Object
DateTime d1 = new DateTime(c.getTime());
DateTime d2 = new DateTime(d.getTime());
// Convert d1 and d2 to LocalDate say ld1 and ld2 since, Java Date defaults to GMT
ld1.compareTo(ld2);
?既然你用DateTime标记了这个问题,我想你已经用Joda了
...
//Initialize Calendar and Date Object
DateTime d1 = new DateTime(c.getTime());
DateTime d2 = new DateTime(d.getTime());
// Convert d1 and d2 to LocalDate say ld1 and ld2 since, Java Date defaults to GMT
ld1.compareTo(ld2);
?我今天不得不这么做,这篇文章中的答案帮助我解决了我的问题。我知道我所有的时区都和老年退休金计划一样。我没有在遗留代码中使用Joda time的自由,因此为了其他具有相同条件的人的利益,下面是我如何使用vanilla Java实现的
方法:
java.sql.Time
由于继承自
java.util.Date
。使用此方法,可以创建
java.util.Date
对象,仅表示自
爪哇时代
- 为了进行比较,必须转换所需的
java.util.Calendar
对象生成表示另一个对象的java.util.Date
对象
自Java时代以来的时间
- 由于日期部分现在相等,
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
/**
* Method to convert a calendar object to java's epoch date
* keeping only the time information
*/
public static Date toEpochDate(Calendar calendar) {
return new Date(Time.valueOf(new SimpleDateFormat("HH:mm:ss").format(calendar.getTime())).getTime());
}
public static void main(String[] args) {
// create any calendar object
Calendar someTime = Calendar.getInstance();
someTime.set(Calendar.HOUR_OF_DAY, 17);
someTime.set(Calendar.MINUTE, 0);
someTime.set(Calendar.SECOND, 0);
// convert it to java epoch date
Date someDate = toEpochDate(someTime);
// create a date object from java.sql.Time
Date fromSqlTime = new Date(Time.valueOf("17:00:00").getTime());
// now do the comparison
System.out.println("Some Date: " + someDate.toString());
System.out.println("Sql Time: " + fromSqlTime.toString());
System.out.println("Are they equal? " + someDate.equals(fromSqlTime));
}
}
Some Date: Thu Jan 01 17:00:00 EST 1970
Sql Time: Thu Jan 01 17:00:00 EST 1970
Are they equal? true