Python每日一记173>>>Java时间日期类之Date类

/*
 * 正如python中的时间日期的处理与其他数据处理不一样的道理一样,java也有专门的日期时间类;
 * 主要的有Date类,Calendar类以及Java8新的日期时间相关类;
 * 这里先记录Date类
*/
package myfirst_Java;
//必要的类
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Hello {
		//主程序入口
	public static void main(String[] args) throws ParseException {
		Date now1=new Date();//返回当前时间
		System.out.println(now1);
		System.out.println(now1.getTime());//获取当前时间距离1970/1/1 00:00:00的毫秒数
		Date date=new Date(1234322L);//指定标准时间以来的毫秒数表示的日期时间,注意时long类型,后面要加L
		System.out.println(date);
		date.setTime(1299993847L);//新设立时间,标准时间以来的毫秒数表示的日期时间
		System.out.println(date);
		System.out.println(date.after(now1));//判断date是否在now1之后
		System.out.println(date.before(now1));//判断date是否在now1之前
//		比较顺序,如果date在now1之后,返回大于0的值,如果date在now1之前则返回小于0的值
//		如果两值相等,则返回0
		System.out.println(date.compareTo(now1));
//		将日期格式化为我们熟悉的日期格式,变成字符串形式
		DateFormat now2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(now2.format(now1));
		
		now2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//只要主框架不变,即那些字母不变
		System.out.println(now2.format(now1));
		
//		将字符串形式转化为日期格式
		String a="2019-10-07 20:57:32";
		DateFormat now3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(now3.parse(a));
		
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值