eg1:
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
date = calendar.getTime();
System.out.println(date);
eg2:
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
localDate = localDate.minusDays(-1);
System.out.println(localDate);
Calendar单例还是多例
package com.czxy;
import java.util.Calendar;
public class LeakTest {
/**
* @param args
*/
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
Runtime run1 = Runtime.getRuntime();
Runtime run2 = Runtime.getRuntime();
System.out.println("c1.equals(c2) " + c1.equals(c2));
System.out.println("c1 == c2 is " + (c1 == c2));
System.out.println("run1.equals(run2) is " + run1.equals(run2));
System.out.println("run1 == run2 is " + (run1 == run2));
}
}
结果
单例模式表示返回的永远是同一个对象
,即对象的引用是相同的。
c1 == c2 false
说明Calendar 不是单例模式。从实际情况分析也应该不是单例,如果c1、c2是同一个对象,那么你修改c1时会影响到c2,时间就没办法比较了。