Operations on dates in Java, Comparison of dates, plus and minus

Source: Internet
Author: User
Tags dateformat

The first two classes to introduce to you are SimpleDateFormat and calendar

A: SimpleDateFormat dateformat=new simpledateformat ("yyyy-mm-dd hh:mm:ss")/

There is also a new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Loc ale.uk)//The output looks like Wed Mar 00:00:00 CST 2018

The parameter is the format of the date you need to format, this is important, remember the month is the uppercase mm, if you write in lowercase, the class will think of it as you want to take the time

Minutes, of course, means that the minutes can not be written in uppercase.

This class provides two relatively important methods:

1.dateformat.format (date);

This date is the format of the time, formatted with a string, the form of the string is of course the format of the argument of this class.

2.dateformat.parse (datestring);

The entry parameter is a string representing the time, returning an object of a time type.

Second: Calendar class: Mainly used to deal with time, add and subtract, get a variety of time

Calendar calendar=calendar.getintance ();

Calendar.settime (date);//date is a date format that puts the time you need to process it in.

(1) The increase and decrease of the date < year, month and day >

Calendar.add (calendar. year,-3);

First, the location of the red is to indicate that you want to add and subtract the part of your date, the date of the addition and subtraction of the benchmark is not the same, they can be the following is my situation:

Year to years plus minus

Date to day plus minus

Hour_of_day hours plus minus

Day_of_week_in_month to the week of the month. For example: 2018-02-27 and then minus one is 2018-02-20.

There is also the Calendar.get (temp) method, which is used to obtain information from a specific date

For example: Temp=calendar. Day_of_week //Days of the week, note that Saturday is the first day of the week

Temp=calendar. Week_of_month //Get this date is the first few weeks of this month.

Temp=calendar. Date//Get this day is the day of the month

  

Operations on dates in Java, Comparison of dates, plus and minus

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.