结合 java.util.Dates 创建日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1260112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:51:01  来源:igfitidea点击:

Combining java.util.Dates to create a date-time

javadatetime

提问by Adamski

I have current have two UI components used to specify a date and a time. Both components return java.util.Dateinstances representing the calendar date and time respectively. My question is:

我目前有两个 UI 组件用于指定日期和时间。这两个组件分别返回java.util.Date表示日历日期和时间的实例。我的问题是:

What is the best way to combine these values to create a java.util.Dateinstance representing the date and time? I would like to avoid dependencies on Joda or other 3rd party libraries.

组合这些值以创建java.util.Date表示日期和时间的实例的最佳方法是什么? 我想避免对 Joda 或其他 3rd 方库的依赖。

My current solution looks like this (but is there a better way?):

我目前的解决方案看起来像这样(但有更好的方法吗?):

Date date = ... // Calendar date
Date time = ... // Time

Calendar calendarA = Calendar.getInstance();
calendarA.setTime(date);

Calendar calendarB = Calendar.getInstance();
calendarB.setTime(time);

calendarA.set(Calendar.HOUR_OF_DAY, calendarB.get(Calendar.HOUR_OF_DAY));
calendarA.set(Calendar.MINUTE, calendarB.get(Calendar.MINUTE));
calendarA.set(Calendar.SECOND, calendarB.get(Calendar.SECOND));
calendarA.set(Calendar.MILLISECOND, calendarB.get(Calendar.MILLISECOND));

Date result = calendarA.getTime();

采纳答案by dfa

public Date dateTime(Date date, Date time) {
    return new Date(
                     date.getYear(), date.getMonth(), date.getDay(), 
                     time.getHours(), time.getMinutes(), time.getSeconds()
                   );
}

you can corvert this deprecated code to Calendar obtaining your solution.

您可以将此弃用的代码转换为 Calendar 以获得您的解决方案。

Then my answer is: no, you cannot do better without using joda

那么我的回答是:不,不使用 joda 就不能做得更好

NB

NB

jodatime soon will be standardized with JSR 310

jodatime 很快将被JSR 310标准化

回答by Rich Seller

I think you're approach is the best you're likely to get without using Joda time. A solution using SimpleDateFormats might use fewer lines, but is not really giving you any benefit.

我认为您的方法是不使用 Joda 时间的最佳方法。使用 SimpleDateFormats 的解决方案可能使用更少的行,但并没有真正给您带来任何好处。

回答by jcarlos78

Using Calendar

使用日历

public Date dateTime(Date date, Date time) {

    Calendar aDate = Calendar.getInstance();
    aDate.setTime(date);

    Calendar aTime = Calendar.getInstance();
    aTime.setTime(time);

    Calendar aDateTime = Calendar.getInstance();
    aDateTime.set(Calendar.DAY_OF_MONTH, aDate.get(Calendar.DAY_OF_MONTH));
    aDateTime.set(Calendar.MONTH, aDate.get(Calendar.MONTH));
    aDateTime.set(Calendar.YEAR, aDate.get(Calendar.YEAR));
    aDateTime.set(Calendar.HOUR, aTime.get(Calendar.HOUR));
    aDateTime.set(Calendar.MINUTE, aTime.get(Calendar.MINUTE));
    aDateTime.set(Calendar.SECOND, aTime.get(Calendar.SECOND));

    return aDateTime.getTime();
}