java的date格式转换_Java中Date日期字符串格式的各种转换

public classDateParserT {/**

* Date 与  String、long 的相互转换

* @param args*/public static voidmain(String[] args) {

Date dt=newDate();

System.out.println(dt); //格式: Wed Jul 06 09:28:19 CST 2016//格式:2016-7-6

String formatDate = null;

formatDate=DateFormat.getDateInstance().format(dt);

System.out.println(formatDate);//格式:2016年7月6日 星期三

formatDate =DateFormat.getDateInstance(DateFormat.FULL).format(dt);

System.out.println(formatDate);//格式 24小时制:2016-07-06 09:39:58

DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制;

formatDate =dFormat.format(dt);

System.out.println(formatDate);//格式12小时制:2016-07-06 09:42:44

DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小时制;

formatDate =dFormat12.format(dt);

System.out.println(formatDate);//格式去掉分隔符24小时制:20160706094533

DateFormat dFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");

formatDate=dFormat3.format(dt);

System.out.println(formatDate);//格式转成long型:1467770970

long lTime = dt.getTime() / 1000;

System.out.println(lTime);//格式long型转成Date型,再转成String:  1464710394 -> ltime2*1000 -> 2016-05-31 23:59:54

long ltime2 = 1464710394;

SimpleDateFormat lsdFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date lDate= new Date(ltime2*1000);

String lStrDate=lsdFormat.format(lDate);

System.out.println(lStrDate);//格式String型转成Date型:2016-07-06 10:17:48 -> Wed Jul 06 10:17:48 CST 2016

String strDate = "2016-07-06 10:17:48";

SimpleDateFormat lsdStrFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date strD=lsdStrFormat.parse(strDate);

System.out.println(strD);

}catch(ParseException e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值