格式化时间日期(Java)

工作中遇到的一个问题,顺便记录一下。

假设获取到的时间日期的值为一个String类型,例如:String time_now = "2018-4-28  17:04:05";

当我们需要单独取前面的日期或者后面的时间的时候,我们需要经过处理:

private String getTime(String end) {
		String time = null;
		if(end == null) {
			return "";
		}else {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date d1 = df.parse(end);
			time = df.format(d1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time; 
		}
	}
由Date获取当前的时间的标准输出为:

Date ss= new Date();

Sat Apr 28 17:17:22 CST 2018    这样的数据

经过这样的格式化处理之后会得到标准的String类型的时间:

        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String time = format0.format(ss.getTime()); 
        System.out.println("格式化0:" + time);  
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
        time = format1.format(ss.getTime());  
        System.out.println("格式化1:" + time);  
输出结果为:格式化结果0:2018-04-28 17:19:55
            格式化结果1:2018年04月28日 17时19分55秒


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值