如何在java中将日历设置为特定日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19921767/
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-08-12 21:32:23  来源:igfitidea点击:

How to set Calender to Particular date and time in java

javatimecalendar

提问by Adi

I am using Calender class of java ..Here calender has been set for day of week and time .Now as per my need i want to set it for date of month and time like calender should be set for 10th of month and time time should be 10 a.m..But i am not able to get it ..

我正在使用 java 的日历类..这里日历已设置为星期几和时间。现在根据我的需要,我想将其设置为月份的日期和时间,例如日历应设置为月份的第 10 天,时间应该设置是上午 10 点。但我无法得到它..

Here is my code..

这是我的代码..

Calendar date = Calendar.getInstance();
    date.set(
            Calendar.DAY_OF_WEEK,
            Calendar.TUESDAY);
    date.set(Calendar.HOUR, 10);
    date.set(Calendar.MINUTE, 58);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);

Please help me.. Thanks in advance..

请帮助我.. 提前致谢..

采纳答案by SudoRahul

If you want set the date of the month, then you could use either of the two, interchangeably.

如果您想设置月份的日期,那么您可以交替使用两者中的任何一个。

date.set(Calendar.DAY_OF_MONTH, 10); // Synonym of DATE
date.set(Calendar.DATE, 10); // Synonym of DAY_OF_MONTH

回答by CHowdappaM

Your code works, only difference is your setting day. but which week of the day?

您的代码有效,唯一的区别是您的设置日。但是哪一周呢?

below code to demonstrate to to set date and time to calender.

下面的代码演示如何设置日历的日期和时间。

Calendar date = Calendar.getInstance();
    long today = date.getTimeInMillis();
    date.set(
            Calendar.DATE,11);
    date.set(Calendar.HOUR, 10);
    date.set(Calendar.MINUTE, 58);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.MONTH, 10);
    date.set(Calendar.YEAR, 2013);
    date.setTimeZone(TimeZone.getDefault());

    long lastday = date.getTimeInMillis();
    System.out.println(date.getTimeInMillis());

    long diff = today - lastday;
    diff = ((diff/1000)/360);
    System.out.println("Hours diff :"+diff);

回答by Basil Bourque

tl;dr

tl;博士

ZonedDateTime.of( 2016 , 9 , 10 , 10 , 0 , 0 , 0 , ZoneId.of( "Europe/Paris" ) )

Details

细节

The accepted answer by ?.?is correct.

?.?接受的答案 是正确的。

java.time

时间

Here is the way to do the same but with modern java.time classes that supplant the troublesome old legacy date-time classes.

这是执行相同操作的方法,但使用现代 java.time 类取代了麻烦的旧遗留日期时间类。

The http://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.htmlclass represents a moment on the timeline in a particular time zone with a resolution of nanoseconds. Instantiate with all the specific year, month, hour, etc. arguments: of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone).

http://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html类表示在特定时间段的时间表,分辨率上一会儿纳秒。实例化所有特定的年、月、小时等参数:of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone).

ZoneId z = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = ZonedDateTime.of( 2016 , 9 , 10 , 10 , 0 , 0 , 0 , z );

See a String representation of that value in standard ISO 8601format extended by appending name of time zone in square brackets.

请参阅标准ISO 8601格式中该值的字符串表示形式,通过在方括号中附加时区名称进行扩展。

String output = zdt.toString();

2016-09-10T10:00:00+02:00[Europe/Paris]

2016-09-10T10:00:00+02:00[欧洲/巴黎]

If you want Strings generated in other formats, search Stack Overflow for DateTimeFormatter.

如果您希望以其他格式生成字符串,请在 Stack Overflow 中搜索DateTimeFormatter.

About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了旧的麻烦的日期时间类,例如java.util.Date, .Calendar, & java.text.SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to java.time.

现在处于维护模式Joda-Time项目建议迁移到 java.time。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backportand further adapted to Androidin ThreeTenABP(see How to use…).

大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植,并进一步用于安卓ThreeTenABP(见如何使用......)。

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,如IntervalYearWeekYearQuarter,等。