The calendar type of JAVA

Source: Internet
Author: User

Day_of_week_in_month: Meaning 1th to 7th for the first week, 8th to 14th for the second week ...

Package Com.test.calendar;

Import Java.util.Calendar;

Import Org.junit.Before;
Import Org.junit.Test;

public class Calendardemo {
Calendar calendar = null;

@Before
public void Test () {
Calendar = Calendar.getinstance ();
}

Basic usage, date and time of day and week
@Test
public void Test1 () {
Get year
int year = Calendar.get (calendar.year);

To get the month, it takes a range of months to 0~11, so it takes +1 for the month to get the current month value
int month = Calendar.get (calendar.month) + 1;

Get Day
int day = Calendar.get (calendar.day_of_month);

When acquired
int hour = Calendar.get (Calendar.hour);
int hour = Calendar.get (Calendar.hour_of_day); 24-hour indication

Get points
int minute = Calendar.get (Calendar.minute);

Get seconds
int second = Calendar.get (Calendar.second);

Week, English-speaking countries start counting from Sunday
int weekday = Calendar.get (Calendar.day_of_week);

System.out.println ("Now is" + year + "years" + month + "Month" + Day + "days" + hour
+ "when" + Minute + "min" + second + "seconds" + "Week" + weekday);
}

One year later, today
@Test
public void Test2 () {
In the same vein as next month's Today Calendar.add (Calendar.month, 1);
Calendar.add (calendar.year, 1);

Get year
int year = Calendar.get (calendar.year);

Get month
int month = Calendar.get (calendar.month) + 1;

Get Day
int day = Calendar.get (calendar.day_of_month);

System.out.println ("A year later" Today: "+ year +" years "+ month +" Month "+ Day +" Days ");
}

Get the last day of any one month
@Test
public void Test3 () {
Suppose the last day of June
int currentmonth = 6;
First day of July, in fact, here 6 is the Currentmonth variable passed in externally
1
Calendar.set (Calendar.get (calendar.year), currentmonth, 1);

Calendar.add (Calendar.date,-1);

Get Day
int day = Calendar.get (calendar.day_of_month);

System.out.println ("The last day of June is" + days + ");
}

Set Date
@Test
public void Test4 () {
Calendar.set (Calendar.year, 2000);
System.out.println ("Now is" + calendar.get (calendar.year) + "year");

Calendar.set (2008, 8, 8);
Get year
int year = Calendar.get (calendar.year);

Get month
int month = Calendar.get (calendar.month);

Get Day
int day = Calendar.get (calendar.day_of_month);

System.out.println ("Now is" + year + "years" + month + "Month" + Day + "Days");
}
}

Output Result:

1 is now November 7, 2016 11:42 18 second week 221 years later today: November 7, 2017 3 June The last day is 30th number 4 is now 2000 5 is now August 8, 2008

The calendar type of JAVA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.