How to add and subtract the date JAVA

Transfer product is slightly Library: http://www.pinlue.com/article/2020/03/0807/009994161333.html

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

* JAVA date subtraction method

*

* @Author old black bamboo (Java Century Network, java2000.net)

*/

public class Test {

public static void main(String[] args) throws Exception {

Calendar calendar = Calendar.getInstance();

calendar.setTime(new Date());

System.out.println (calendar.get (Calendar.DAY_OF_MONTH)); // today's date

calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);// 让日期加1

System.out.println (calendar.get (Calendar.DATE)); // add date after 1 Top

Date d = new Date();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

System.out.println ( "Today's date:" + df.format (d));

System.out.println ( "Two days before the:" + df.format (new Date (d.getTime () - 2 * 24 * 60 * 60 * 1000)));

System.out.println ( "date after three days:" + df.format (new Date (d.getTime () + 3 * 24 * 60 * 60 * 1000)));

}

}

 

Published 60 original articles · won praise 52 · views 110 000 +

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/104729841