标签:set art public fda 描述 amp java end 代码生成
public String toString() //返回值为 对象的类型+@+内存地址值
class Demo{ private String name; private int age; @Override public String toString() { return "Demo{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
public boolean equals(Object obj) //默认比较的是对象的内存地址,只要不是同一个对象必然是false
覆写equals方法,使其比较对象内容是否完全相同。在IntelliJ IDEA中,可以使用快捷键alt+insert,并选择equals() and hashCode()进行自动代码生成。
public class ObjectDemo01 { private String name; private int age; @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ObjectDemo01)) return false; ObjectDemo01 that = (ObjectDemo01) o; return getAge() == that.getAge() && Objects.equals(getName(), that.getName()); } }
System.out.println(new Date()); //返回值:Tue Nov 26 23:02:07 CST 2019
Date date01 = new Date(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str01 = format.format(date01); System.out.println(str01);
parse:将字符串解析为Date对象
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日"); String str02 = "2018年12月11日"; Date date02 = df.parse(str02); System.out.println(date02);
public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); int hourOfDay = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); System.out.print(year + "年" + month + "月" + dayOfMonth + "日" + hourOfDay + "时" + minute + "分" + second + "秒"); }
public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int year1 = cal.get(Calendar.YEAR); int month1 = cal.get(Calendar.MONTH) + 1; int dayOfMonth1 = cal.get(Calendar.DAY_OF_MONTH); System.out.println(year1 + "年" + month1 + "月" + dayOfMonth1 + "日"); // 使用add方法 cal.add(Calendar.DAY_OF_MONTH, 2); // 加2天 cal.add(Calendar.YEAR, -70); // 减3年 int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); System.out.println(year + "年" + month + "月" + dayOfMonth + "日"); }
Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); System.out.println(date);
long start = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共耗时毫秒:" + (end - start));
// 新建StringBuilder。初始值可有可无 StringBuilder sb = new StringBuilder("itcast|||"); // public StringBuilder append(...):添加任意类型数据的字符串形式,并返回当前对象自身 StringBuilder sbn = sb.append("hello,").append("world!").append(true).append(100); // public String toString():将当前StringBuilder对象转换为String对象 String str = sbn.toString(); System.out.println(str);
Object类、日期时间类、system类及StringBuilder字符串容器
标签:set art public fda 描述 amp java end 代码生成
原文地址:https://www.cnblogs.com/fanbao/p/11939307.html