(java)日期类型

1.java.lang包下得System类 下得
static long currentTimeMillis() Returns the current time in milliseconds.
方法
计算从1970-1-1到现在得毫秒数

2.获取系统当前时间
java.util下面的Date类,这个需要导包

impot java.util.Date
public class Test{
	public static void main(String[] args){
			Date nowTime = new Date();
			System.out.println(nowTime);//Fri Feb 28 17:46:35 CST 2020
			//以上程序输出的时间,对于是英文不易于理解
			

			//所以引入“格式化”日期
			//java.util.Date;--->String
			在java.text.SimpleDateFormat 类下有定义
			/*
			y 年
			M 月
			d 日
			H 小时
			m 分
			s 秒
			S 毫秒
			*/
			//1.创建日期格式化对象
			
			SimpleDateFormat ds = new SimpleDateFormat("y年M月d日 H:m:s  S");
				String s1 = ds.format(nowTime);
				System.out.println(s1);//2020年2月28日 17:56:10  804
	}
}

3.获取特定的日期

public class Test{
	public static void main(String[] args){
		String Time = "2008年8月8日  8:8:8"

		//将String日期转换成日期类型Date
		//String-->Date

		//1.创建日期格式化对象
		SimpleDateFormat ds = new SimpleDateFormat("y年M月d日 H:m:s  S");//格式不能随意写,要和像转换日期的相同
		//将字符串转换成日期类型
		 Date d1 = ds.parse(Time); //这里注意,格式必须一一对应,否则运行会报错误
	}

}

4.Date d = new Date();

// 获取系统当前时间的
Date d2 = new Date(System.currentTimeMillis());//2020/2/28 下午8:59(要想输出这样的格式,前面还需要格式化日期)

//以1970-1-1 08:00:00  加上Date里的毫秒数
Date t1 =  new Date(1000);

5.日历

import java.util.Calendar
public class Test{
	public static void main(String[] args){
		//获取系统当前日历
		Calendar c = Calendar.getInstance();

		//查看当前日历的“星期几”
		int i = c.get(Calendar.Day_OF_WEEK);
		//Day_OF_WEEK作为常量字段名,代表了当前日期为周几
		System.out.println(i);//3(中国星期日,外国看作第一天)

		//获取2008 8  8 是星期几
		//获取2008 8 8 的日历
		Stirng Time = "2008,08,08";
		//将String转换为Date类型
		SimpleDateFormat f1 = new SimpleDateFormat("yyyy,MM,dd");
		Date d3 = f1.parse(Time);
		//这里就使得c为2008 08 08 的日历了
		c.setTime(d);
		//即可获取20080808 是 星期几
		System.out.println(Calendar.DAY_OF_WEEK);
		
		}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值