0
点赞
收藏
分享

微信扫一扫

Java成神路 —— 时间日期类


文章目录

  • ​​1. Date类​​
  • ​​1.1 Date类概述​​
  • ​​1.2 Date类构造方法​​
  • ​​1.3 示例代码​​
  • ​​2. Date类常用方法​​
  • ​​2.1 常用方法​​
  • ​​2.2 示例代码​​
  • ​​2.3 SimpleDateFormat类​​
  • ​​1. SimpleDateFormat类概述​​
  • ​​2. SimpleDateFormat类构造方法​​
  • ​​3. SimpleDateFormat类的常用方法​​
  • ​​4. 示例代码​​
  • ​​2.4 Calendar类​​
  • ​​1. Calendar类概述​​
  • ​​2. Calendar类常用方法​​
  • ​​3. 示例代码​​

1. Date类

1.1 Date类概述

  • Date 代表了一个特定的时间,精确到毫秒

1.2 Date类构造方法

方法名

说明

public Date()

分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒

public Date(long date)

分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数

1.3 示例代码

import java.util.Date;

public class Demo {
public static void main(String[] args) {

//public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
Date d1 = new Date();
System.out.println(d1);

//public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
long date = 1000*60*60;
Date d2 = new Date(date);
System.out.println(d2);
}
}

2. Date类常用方法

2.1 常用方法

方法名

说明

public long getTime()

获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值

public void setTime(long time)

设置时间,给的是毫秒值

2.2 示例代码

import java.util.Date;

public class Demo {
public static void main(String[] args) {

//创建日期对象
Date date = new Date();

//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
System.out.println(date.getTime());
System.out.println(date.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 +"年");

//public void setTime(long time):设置时间,给的是毫秒值
//long time = 1000*60*60;
long time = System.currentTimeMillis();
date.setTime(time);
System.out.println(date);
}
}

2.3 SimpleDateFormat类

1. SimpleDateFormat类概述

  • SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

2. SimpleDateFormat类构造方法

方法名

说明

public SimpleDateFormat()

构造一个SimpleDateFormat,使用默认模式和日期格式

public SimpleDateFormat(String pattern)

构造一个SimpleDateFormat使用给定的模式和默认的日期格式

3. SimpleDateFormat类的常用方法

  • 格式化(从Date到String)
    ​​​public final String format(Date date)​​:将日期格式化成日期/时间字符串
  • 解析(从String到Date)
    ​​​public Date parse(String source)​​:从给定字符串的开始解析文本以生成日期

4. 示例代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {
public static void main(String[] args) throws ParseException {

//格式化:从 Date 到 String
Date d = new Date();

// SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("--------");

//从 String 到 Date
String ss = "2048-08-09 11:11:11";

//ParseException
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}

2.4 Calendar类

1. Calendar类概述

  • Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
  • Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。该方法返回一个Calendar 对象。
  • 其日历字段已使用当前日期和时间初始化:​​Calendar rightNow = Calendar.getInstance();​

2. Calendar类常用方法

方法名

说明

public int get(int field)

返回给定日历字段的值

public abstract void add(int field, int amount)

根据日历的规则,将指定的时间量添加或减去给定的日历字段

public final void set(int year,int month,int date)

设置当前日历的年月日

3. 示例代码

import java.util.Calendar;

public class Demo {
public static void main(String[] args) {

//获取日历类对象
Calendar c = Calendar.getInstance();

//public int get(int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}


举报

相关推荐

0 条评论