java中你容易忘记的基础大盘点_java.lang.Date

日期格式化

package com.ruge.modules.sys.controller;

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

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 武健
 * \* Date: 2018/1/30
 * \* Time: 22:17
 * \* To change this template use File | Settings | File Templates.
 * \* Description:日期格式化
 * \
 */
public class TestDate {
    public static void main(String[] args) throws ParseException {
        /**
         * ---日期-->文本
         */
        Date date = new Date();
        /**
         * java.text.SimpleDateFormat
         * 易于国际化
         */
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat();
        /**
         *  格式化 1
         * 18-1-30 下午10:45
         */
        String format1 = simpleDateFormat1.format(date);
        System.out.println(format1);
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        /**
         * 2018-01-30 22:48:19
         */
        String format2 = simpleDateFormat2.format(date);
        System.out.println(format2);
        /**
         * 文本-->日期
         */
        /**
         * Tue Jan 30 22:45:00 CST 2018
         */
        Date parse1 = simpleDateFormat1.parse("18-1-30 下午10:45");
        System.out.println(parse1);
        /**
         * Sun Dec 31 22:48:19 CST 2017
         */
        Date parse2 = simpleDateFormat2.parse("2018-01-30 22:48:19");
        System.out.println(parse2);
        /**
         * 计算两个字符串之间的天数
         */
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
        String time1 = "2017-10-10";
        String time2 = "2017-12-12";
        Date parse3 = simpleDateFormat3.parse(time1);
        Date parse4 = simpleDateFormat3.parse(time2);
        long i =(parse4.getTime() - parse3.getTime());
        System.out.println(i);
        /**
         * 间隔=Date1.getTime()-Date2.getTime();得出来的是毫秒数.
         * 除1000是秒,再除60是分,再除60是小时..............................
         */
        long time = i/(1000 * 60 *60 * 24);
        System.out.println(time);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值