简体   繁体   English

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

[英]How to set Calender to Particular date and time in java

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 am.But i am not able to get it .. 我正在使用Java的Calender类..这里将日历设置为星期几和时间。现在根据我的需要,我想将它设置为月份和时间的日期,例如应该将日历设置为月份的第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.. 请帮助我..预先感谢..

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

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);

tl;dr tl; dr

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

Details 细节

The accepted answer by Ɍ.Ɉ is correct. Ɍ.Ɉ接受的答案是正确的。

java.time 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.html class represents a moment on the timeline in a particular time zone with a resolution of nanoseconds . http://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html类表示特定时区中时间轴上的时刻,分辨率为纳秒 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) . 用所有特定的年,月,时等参数实例化: 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 8601 format 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.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the old troublesome date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat . 这些类取代了麻烦的旧日期时间类,例如java.util.Date.Calendarjava.text.SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to java.time. 现在处于维护模式Joda-Time项目建议迁移到java.time。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use… ). 大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植 ,并进一步适应的AndroidThreeTenABP (见如何使用...... )。

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more. 您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuarter等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM