Common Java Date Methods collection (Part One)

Source: Internet
Author: User
Tags date current time

1. Calculate the maximum number of days in a January:

Calendar time=calendar.getinstance ();  
time.clear ();
Time.set (calendar.year,year);//year is int
time.set (calendar.month,i-1);/Note that Calendar object defaults to January of 0 
  
   int Day=time.getactualmaximum (calendar.day_of_month);//Days of the Month
  

Note: Before using the Set method, you must clear it, otherwise many of the information will be inherited from the system current time

Conversion of 2.Calendar and date:

(1) Calendar conversion to date:

Calendar cal=calendar.getinstance ();
Date date=cal.gettime ();

(2) Date converted to calendar:

Date Date=new date ();  
Calendar cal=calendar.getinstance ();
cal.settime (date);

3. Format the output date time (more of this):

Date Date=new date ();  
SimpleDateFormat df=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");  
String Time=df.format (date);
System.out.println (time);

4. Calculation of the first week of the year:

(1) to calculate the day of the year is the first week:

Calendar cal=calendar.getinstance ();  
Cal.set (Calendar.year, 2006);  
Cal.set (Calendar.month, 8);  
Cal.set (Calendar.day_of_month, 3);
int Weekno=cal.get (calendar.week_of_year);

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.