17 Examples Of Calendar Together With Appointment Inwards Java

The java.util.Calendar cast was added inward Java on JDK 1.4 inward an endeavour to gear upwards around flaws of the java.util.Date class. It did brand around work simpler, e.g. exercise an arbitrary appointment comes easier using new GregorianCalendar(2016, Calendar.JUNE, 11) constructor, every bit opposed to Date cast where the yr starts from 1900 in addition to Month was starting from zero. It didn't solve all the problems e.g. mutability in addition to thread-safety of Date cast all the same remains, but it does brand life easier at that time. Now alongside Java 8 everything related to Date in addition to Time has expire super slow in addition to consistent but unfortunately, it volition convey around other five to 10 years earlier older version of Java goes away. Don't believe me, in that location are all the same applications running on JDK 1.5 which was released 12 years ago. The bottom draw is it's all the same of import to know almost Date in addition to Calendar inward Java.

The java.util.Calendar cast is an abstract class in addition to y'all obtain the event of a Calendar local to your timezone in addition to Locale yesteryear calling the getInstance() method of java.util.Calendar class. This method by in addition to large returns an event of GregorianCalendar cast except when your JVM is running alongside Japanese in addition to Thai Locale where it returns dissimilar instances of Calendar e.g. BuddhistCalendar for Thai ("th_TH") in addition to JapaneseImperialCalendar for Japanese ("ja_JP") Locale.



The Gregorian calendar is the calendar which is used today. It came into resultant on Oct 15, 1582, inward around countries in addition to after inward other countries. It replaces its predecessor the Julian calendar in addition to 10 days were removed from the calendar, i.e., Oct 4, 1582 (Julian) was followed yesteryear Oct 15, 1582 (Gregorian) due to differences inward how Julian in addition to GregorianCalendar calculates confine year.

In Julian calendar, every iv years is a leap year. In the Gregorian calendar, a confine yr is a yr that is divisible yesteryear 4 but non divisible yesteryear 100, or it is divisible yesteryear 400, i.e., the Gregorian calendar omits century years which are non divisible yesteryear 400 (removing iii confine years (or iii days) for every 400 years). Furthermore, Julian calendar considers the start 24-hour interval of the yr every bit march 25th, instead of Jan 1st.

The Calendar cast provides back upwards for extracting appointment in addition to fourth dimension fields from a Date e.g. YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, SECOND, MILLISECOND; every bit good every bit manipulating these fields e.g. yesteryear adding in addition to subtracting days from a appointment in addition to calculating side yesteryear side or previous day, calendar month or yr (see) etc. In this article, I'll portion around useful Calendar illustration to empathise how to usage it for your daily appointment in addition to time-related operations.




How to convert Date to Calendar inward Java

One of the start affair a Java developer should know piece using Calendar is the conversion betwixt Date in addition to Calendar classes. Similar to how y'all converted Timestamp to Date, hither besides nosotros tin usage the getTime() in addition to setTime() method, albeit this fourth dimension they are from the java.util.Calendar class.

By default, when y'all acquire the Calendar it has today's date, to laid to whatever arbitrary appointment but telephone outcry upwards the Calendar.setTime(date), straightaway y'all receive got the correct appointment laid to your calendar in addition to y'all tin usage the Calendar's get() in addition to add() method to extract dissimilar fields in addition to manipulate them every bit good e.g. day, calendar month in addition to year.

Date date= new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date);

Similarly, to convert a java.util.Calendar dorsum to the java.util.Date but telephone outcry upwards the getTime() method of Calendar cast every bit shown inward the next example:

Date d = cal.getTime();

Super slow right? but solely if y'all know. It's non real obvious from API every bit Calendar doesn't receive got a Date value inward the constructor in addition to the cite getTime() in addition to setTime() besides doesn't betoken that they render or receive got a Date value. If y'all desire to acquire to a greater extent than almost Date in addition to Calendar classes inward Java, I advise reading Core Java Volume 1 - Fundamentals yesteryear Cay S. Horstmann, 1 of the improve books to acquire meat Java concepts.

 inward an endeavour to gear upwards around flaws of the  17 Examples of Calendar in addition to Date inward Java


How to extract day, calendar month in addition to other fields from Calendar

Once y'all laid the Date to Calendar class, y'all tin usage its diverse get() method to extract dissimilar fields e.g. Calendar.get(Calendar.DAY_OF_MONTH) to acquire the current day. Here is a listing of Calendar plain y'all tin usage alongside get() in addition to add() method:

get(Calendar.DAY_OF_WEEK): returns 1 (Calendar.SUNDAY) to seven (Calendar.SATURDAY).
get(Calendar.YEAR): returns year
get(Calendar.MONTH): returns 0 (Calendar.JANUARY) to eleven (Calendar.DECEMBER).
get(Calendar.DAY_OF_MONTH), get(Calendar.DATE): 1 to 31
get(Calendar.HOUR_OF_DAY): 0 to 23
get(Calendar.MINUTE): 0 to 59
get(Calendar.SECOND): 0 to 59
get(Calendar.MILLISECOND): 0 to 999
get(Calendar.HOUR): 0 to 11, to move used together alongside Calendar.AM_PM.
get(Calendar.AM_PM): returns 0 (Calendar.AM) or 1 (Calendar.PM).

This is real of import to know because in 1 trial y'all know that y'all tin extract whatever appointment or time-related plain from Calendar instance. The Java How to Program yesteryear Dietel in addition to Dietel besides receive got som to a greater extent than practical examples of accessing appointment in addition to time-related attributes from Calendar instance.

 inward an endeavour to gear upwards around flaws of the  17 Examples of Calendar in addition to Date inward Java


How to add together days, calendar month in addition to yr to Date

You tin usage the add() method of Calendar to add together or subtract a day, month, year, hour, a infinitesimal or whatever other plain from Date inward Java. Yes, the same method is used for adding in addition to subtracting, y'all but ask to transcend a negative value for subtraction e.g. -1 to subtract 1 24-hour interval in addition to acquire the yesterday's 24-hour interval every bit shown inward the next example:

cal2.add(Calendar.DAY_OF_MONTH, 1); d = cal2.getTime(); System.out.println("date after adding 1 24-hour interval (tomorrow) : " + d);  cal2.add(Calendar.DAY_OF_MONTH, -2); d = cal2.getTime(); System.out.println("date after subtracting ii 24-hour interval (yesterday) : " + d);  Output appointment after adding 1 24-hour interval (tomorrow): quarta-feira Jun 22 00:00:00 IST 2016 appointment after subtracting ii days (yesterday): Monday Jun 20 00:00:00 IST 2016

You tin encounter that the value of appointment returned yesteryear the Calendar is dissimilar after adding in addition to subtracting ii days from calendar event using the add() method. Just remember, to subtract a day, transcend a negative value e.g. to subtract ii days nosotros passed -2. If y'all desire to subtract but 1 day, transcend -1

 inward an endeavour to gear upwards around flaws of the  17 Examples of Calendar in addition to Date inward Java



Java Calendar Example 

Here is our Java programme to demonstrate around to a greater extent than illustration of Calendar cast inward Java.

import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;  /*  * Java Program to demo how to usage Calendar to acquire a dissimilar appointment in addition to time-related attribute.  * This programme contains multiple how to exercise examples of Calendar cast to learn y'all how  * to acquire day, month, year, hour, infinitesimal or whatever other appointment in addition to fourth dimension realted attributes from  * Calendar instance.   */  public class Main {  public static void main(String[] args) {  Calendar cal = Calendar.getInstance(); // returns GregorianCalendar except Nippon in addition to Thailand  // Example 1 - acquire the yr from Calendar System.out.println("Year : " + cal.get(Calendar.YEAR));  // Example 2 - acquire the calendar month from Calendar System.out.println("Month : " + cal.get(Calendar.MONTH));  // Example iii - acquire the Day of calendar month from Date in addition to Calendar System.out.println("Day of Month : " + cal.get(Calendar.DAY_OF_MONTH));  // Example 4 - acquire the Day of Week from Date System.out.println("Day of Week : " + cal.get(Calendar.DAY_OF_WEEK));  // Example five - acquire the Day of yr from Date System.out.println("Day of Year : " + cal.get(Calendar.DAY_OF_YEAR));  // Example half dozen - acquire the Week of Year from Calendar System.out.println("Week of Year : " + cal.get(Calendar.WEEK_OF_YEAR));  // Example seven - acquire the Week of Month from Date System.out.println("Week of Month : " + cal.get(Calendar.WEEK_OF_MONTH));  // Example 8 - acquire the hr from time System.out.println("Hour : " + cal.get(Calendar.HOUR));  // Example ix - acquire the AM PM from time System.out.println("AM PM : " + cal.get(Calendar.AM_PM));  // Example 10 - acquire the hr of the 24-hour interval from Calendar System.out.println("Hour of the Day : " + cal.get(Calendar.HOUR_OF_DAY));  // Example eleven - acquire the infinitesimal from Calendar System.out.println("Minute : " + cal.get(Calendar.MINUTE));  // Example 12 - acquire the Second from Calendar System.out.println("Second : " + cal.get(Calendar.SECOND)); System.out.println();  // Example xiii - converting Date to Calendar Date date= new Date(); Calendar cal1 = Calendar.getInstance(); cal.setTime(date);  // Example fourteen - Creating GregorianCalendar of specific date Calendar cal2 = new GregorianCalendar(2016, Calendar.JUNE, 21);  // Example xv - Converting Calendar to Date Date d = cal2.getTime(); System.out.println("date from Calendar : " + d);  // Example xvi - adding 1 24-hour interval into date cal2.add(Calendar.DAY_OF_MONTH, 1); d = cal2.getTime(); System.out.println("date after adding 1 24-hour interval (tommorrow) : " + d);  // Example 17 - subtracting a 24-hour interval from day cal2.add(Calendar.DAY_OF_MONTH, -2); d = cal2.getTime(); System.out.println("date after subtracting ii 24-hour interval (yesterday) : " + d); }  }  Output Year: 2016 Month: 5 Day of Month: 17 Day of Week: 6 Day of Year: 169 Week of Year: 25 Week of Month: 3 Hour: 4 AM PM: 1 Hour of the Day: 16 Minute: 24 Second: 37  appointment from Calendar : Tue Jun 21 00:00:00 SGT 2016 appointment after adding 1 24-hour interval (tommorrow) : quarta-feira Jun 22 00:00:00 SGT 2016 appointment after subtracting ii 24-hour interval (yesterday) : Monday Jun 20 00:00:00 SGT 2016

That's all almost how to usage Calendar cast inward Java. It's a useful cast for extracting in addition to manipulating appointment in addition to time-related fields inward Java half dozen in addition to Java seven but alongside Java 8 it's value is diminished. You should solely usage this cast if y'all are non running inward Java 8. In Java 8 y'all should usage the LocalDate methods to extract appointment related values, in addition to LocalTime's time-related methods to extract fourth dimension values e.g. hour, minutes in addition to seconds. You tin besides read Java 8 inward Action to acquire to a greater extent than almost novel Date in addition to Time API of Java 8.

Further Learning
Complete Java Masterclass
tutorial
  • How to parse String to LocalDateTime inward Java 8? (tutorial)
  • How to convert java.util.Date to java.sql.Timestamp inward JDBC? (tutorial)
  • How to parse String to LocalDateTime inward Java 8? (tutorial
  • How to acquire electrical flow appointment in addition to fourth dimension inward Java 6? (tutorial
  • How to calculate the departure betwixt ii dates inward Java? (example)
  • How to convert String to LocalDateTime inward Java 8? (example
  • How to compare ii dates inward Java? (tutorial
  • How to convert Date to LocalDateTime inward Java 8? (tutorial
  • How to acquire electrical flow Timestamp value inward Java? (tutorial
  • How to parse String to Date using JodaTime library? (example
  • How to convert java.util.Date to java.sql.Date inward JDBC? (tutorial
  • How to convert java.util.Date to java.time.LocalDateTime inward Java 8? (tutorial)

  • Thanks for reading this article, if y'all similar this tutorial, delight portion alongside your friends in addition to colleagues. If y'all receive got whatever feedback or suggestions in addition to hence delight drib a comment. If y'all receive got whatever interesting example, which is non inward this article, experience gratuitous to portion them alongside us. 

    Komentar

    Postingan populer dari blog ini

    Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

    5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons

    Top V Websites For Practicing Information Structures Together With Algorithms For Coding Interviews Free