This article describes the method of Java using DateUtils to perform mathematical operations on dates. Share it for your reference, as follows:
Recently, I am writing a program for uploading data and need to perform some mathematical operations on Date. I personally feel that in Java, mathematical operations on dates are still quite common, so I played Date's mathematical operations. After trying it out, I found that the DateUtils tool class is very convenient for Date's mathematical operations, see the code.
package date;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.lang3.time.DateUtils;public class DateCalculate { /** * Operation of date format* @param args */ public static void main(String[] args) { System.out.println("Wulin.com test result:"); Date now = new Date(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("-----------------: " + sd.format(now)); //Year: Add and subtract operations System.out.println("1 year later: "+sd.format(DateUtils.addYears(now, 1))); System.out.println("1 year ago: "+sd.format(DateUtils.addYears(now, -1))); //Moon: Add and subtract operations System.out.println("1 month later: "+sd.format(DateUtils.addMonths(now, 1))); System.out.println("1 month ago: "+sd.format(DateUtils.addMonths(now, -1))); //Week: Add and subtract operations System.out.println("1 week later: "+sd.format(DateUtils.addWeeks(now, 1))); System.out.println("1 week ago: "+sd.format(DateUtils.addWeeks(now, -1))); //Type: Add and subtract operations System.out.println("Yesterday's time: " + sd.format(DateUtils.addDays(now, -1))); System.out.println("This time tomorrow: " + sd.format(DateUtils.addDays(now, 1))); //Hours: add and subtract operations System.out.println("1 hour later: " + sd.format(DateUtils.addHours(now, 1))); System.out.println("1 hour ago: " + sd.format(DateUtils.addHours(now, -1))); //Minutes: add and subtract operations System.out.println("1 minute later: " + sd.format(DateUtils.addMinutes(now, 1))); System.out.println("1 minute ago: "+sd.format(DateUtils.addMinutes(now, -1))); //Second: Add and subtract operations System.out.println("10 seconds later: "+sd.format(DateUtils.addSeconds(now, 10))); System.out.println("10 seconds ago: "+sd.format(DateUtils.addSeconds(now, -10))); //Milliseconds: Add and subtract operations System.out.println("1000 milliseconds later: "+sd.format(DateUtils.addMilliseconds(now, 1000))); System.out.println("1000 milliseconds before: "+sd.format(DateUtils.addMilliseconds(now, -1000))); }}Running results:
Note: org.apache.commons.lang3.time.DateUtils; is introduced in the code, and an error will be reported when running the program directly! Readers can download the corresponding components at the class library download address provided by the official website, or click here to download this site .
The reference directory structure of the final file is as follows:
PS: Here are a few online tools for your reference:
Online Date/Day Calculator:
http://tools.VeVB.COM/jisuanqi/date_jisuanqi
Online Perpetual Calendar:
http://tools.VeVB.COM/bianmin/wannianli
Online Lunar/Gregorian calendar conversion tool:
http://tools.VeVB.COM/bianmin/yinli2yangli
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
For more information about Java related content, please check out the topics of this site: "Summary of Java Date and Time Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.