JavaEE学习日志持续更新----> 必看!JavaEE学习路线(文章总汇)
Java学习日志(一)
Date类
import java.util.Date;
Date()
获取当前时间
Date date = new Date(); //无参构造
Date(long date)
把毫秒转换成日期
Date date = new Date(0L); //有参构造
参数:
- long date:毫秒值
注意:
- 涉及时间与日期相关的类中使用的long参数都是毫秒
- 时间原点:
- 英国:1970年1月1日 00:00:00(0毫秒)
- 中国:1970年1月1日 08:00:00(0毫秒)
setTime(long time)
毫秒转换成日期,此方法相当于Date类的带参构造
private static void show02() {
Date date = new Date();
date.setTime(0L);
System.out.println(date);
}
getTime();
从时间原点到当前时间的毫秒值
private static void show01() {
Date date = new Date();
long time = date.getTime();
System.out.println(time);
}
DateFormat类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* DateFormat类
* 作用:1.格式化
* 2.解析
* 抽象类,没有构造方法,需用子类SimpleDateFormat
* 成员方法:
* String format(Date date);
* Date parse(String source);
* SimpleDateFormat类:
* SimpleDateFormat(String pattern) 构造一个 SimpleDateFormat使用给定的模式和默认的
* 默认日期格式符号 FORMAT区域设置。
* 参数:
* yyyy-MM-dd HH:mm:ss
* yyyy年MM月dd日 HH时mm分ss秒
* 注意:表示模式的符号不能改变,是一个固定值
* 连接模式的连接符可随意使用
* */
public class Demo01 {
public static void main(String[] args) throws ParseException {
show01();
show02();
}
/*
* Date parse(String source);
* */
private static void show02() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2020-01-29 13:35:13");
System.out.println(date);
}
/*
* 日期-->字符串
* */
private static void show01() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(new Date());
System.out.println(format);
}
}
Object类
toString方法
/*
* java.lang.Object类
* 特点:是所有类的父类,任何类都直接或者间接继承了Object
* 任何一个类都可以使用Object类中的方法
* toString方法
* public String toString() {
return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
}
hashCode():获取一个系统随机给出的十进制整数
Integer.toHexString():把十进制的整数转换为十六进制
* */
public class Demo {
public static void main(String[] args) {
Person p = new Person("AAA", 18);
String s = p.toString();
System.out.println(s);
//直接打印对象的名字,就是调用toString方法
System.out.println(p);
}
}
equals方法
比较两个对象的地址值是否相等
public boolean equals(Object obj) {
return this == obj;
}
重写方法
/*
* 直接打印对象的地址值没有意义,所以重写Object类的toString方法
* */
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/*比较地址没有意义,所以重写Object类的equals方法,让他比较属性*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
Calendar类
java.util.Calendar:日历类
Calendar c = Calendar.getInstance();//多态,创建子类对象
System.out.println(c);
作用:
- 可以通过类中的方法获取对应的日历字段(年,月,日…)
- 可以通过类中的方法修改对应的日历字段
- 可以通过类中的方法对日历字段进行计算
抽象类,无法直接创建对象,提供了一个静态方法getInstance,返回的就是Calendar类的子类对象
注意:
- 西方:0-11月 周日-周六
- 东方:1-12月 周一-周日
get(int field)
int get(int field) 返回给定日历字段的值。
private static void show01() {
Calendar c = Calendar.getInstance();
//1为YEAR字段对应的值
//int year = c.get(1);
int year = c.get(Calendar.YEAR);
System.out.println(year);
int month = c.get(Calendar.MONTH);
System.out.println(month);
int day = c.get(Calendar.DATE);
System.out.println(day);
}
set(int field, int value)
- void set(int field, int value) 将给定的日历字段设置为给定值。
- void set(int year, int month, int date) 设置日历字段的值 YEAR , MONTH和 DAY_OF_MONTH 。
private static void show02() {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,8888);
c.set(Calendar.MONTH,8);
c.set(Calendar.DAY_OF_MONTH,8);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"."+month+"."+day);
//同时设置
c.set(9999,9,9);
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH);
int day1 = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year1+"."+month1+"."+day1);
}
add(int field, int amount)
abstract void add(int field, int amount)
根据日历的规则,将指定的时间量添加或减去给定的日历字段。
private static void show03() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR,10);
c.add(Calendar.MONTH,-3);
c.add(Calendar.DAY_OF_MONTH,10);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year);
System.out.println(month);
System.out.println(day);
}
getTime()
public Date getTime() 把日历转换为Date日期
private static void show04() {
Calendar c = Calendar.getInstance();
Date date = c.getTime();
System.out.println(date);
}
setTime(Date date)
setTime(Date date) 把日期转换为日历
private static void show05() {
Calendar c = Calendar.getInstance();
c.setTime(new Date(0L));
int year = c.get(Calendar.YEAR);
System.out.println(year);
}
练习:计算一个人出生了多少天
package cn.itcast.day01.demo04.Calendar;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
/*
* 计算一个人出生了多少天
* */
public class Demo03test {
public static void main(String[] args) {
/*
* 获取出生年月日
* */
Scanner sc = new Scanner(System.in);
System.out.println("请输入出生的年月日");
int birthYear = sc.nextInt();
int birthMonth = sc.nextInt();
int birthDay = sc.nextInt();
// 获取当前时间的毫秒值
long nowSecond = System.currentTimeMillis();
// 设置日历为出生时间
Calendar c = Calendar.getInstance();
c.set(birthYear, birthMonth, birthDay);
// 把出生日历转化为出生日期
Date birthTime = c.getTime();
// 获取出生日期的毫秒值
long birthSecond = birthTime.getTime();
// 把当前日期的毫秒值与出生日期的毫秒值相减
long distance = nowSecond - birthSecond;
// 把毫秒值转化为天数
double day= distance /1000/3600/24;
System.out.println(day);
}
}