Reputation: 1455
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 ..
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..
Upvotes: 0
Views: 28680
Reputation: 340070
ZonedDateTime.of( 2016 , 9 , 10 , 10 , 0 , 0 , 0 , ZoneId.of( "Europe/Paris" ) )
The accepted answer by Ɍ.Ɉ is correct.
Here is the way to do the same but with modern java.time classes that supplant the troublesome old legacy date-time classes.
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. 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)
.
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.
String output = zdt.toString();
2016-09-10T10:00:00+02:00[Europe/Paris]
If you want Strings generated in other formats, search Stack Overflow for DateTimeFormatter
.
The java.time framework 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
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
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…).
The ThreeTen-Extra project 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.
Upvotes: 2
Reputation: 1620
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);
Upvotes: 5
Reputation: 45080
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
Upvotes: 12