• DateUtils


    package com.vcredit.ddcash.batch.util;

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

    /**
    * 日期处理工具类
    * Created by xutao on 2016/11/3 0003.
    */
    public class DateUtils {

    /**
    * 获取年
    *
    * @param date 参数
    * @return 年
    */
    public static String getYear(Date date) {
    if (date == null) {
    throw new RuntimeException("参数date不能为空!");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    return sdf.format(date);
    }

    /**
    * 获取月
    *
    * @param date 参数
    * @return 月
    */
    public static String getMonth(Date date) {
    if (date == null) {
    throw new RuntimeException("参数date不能为空!");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("MM");
    return sdf.format(date);
    }

    /**
    * 获取日
    *
    * @param date 参数
    * @return 日
    */
    public static String getDay(Date date) {
    if (date == null) {
    throw new RuntimeException("参数date不能为空!");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("dd");
    return sdf.format(date);
    }

    /**
    * 按照指定的格式转换日期
    *
    * @param sourceDate 日期
    * @param pattern 格式
    * @return 日期字符串
    */
    public static String dateToString(Date sourceDate, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(sourceDate);
    }

    /**
    * 按照默认的格式转换日期
    *
    * @param sourceDate 日期
    * @return 日期字符串
    */
    public static String dateToString(Date sourceDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(sourceDate);
    }

    /**
    * 毫秒值转换成日期字符串
    *
    * @param milliseconds 毫秒值
    * @return 日期字符串
    */
    public static String millisToString(Long milliseconds) {
    if (milliseconds == null) {
    throw new RuntimeException("时间(毫秒)不能为空");
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliseconds);
    return dateToString(calendar.getTime());
    }

    /**
    * 按照指定格式转换毫秒值到日期字符串
    *
    * @param milliseconds 毫秒值
    * @param pattern 格式
    * @return 日期字符串
    */
    public static String millisToString(Long milliseconds, String pattern) {
    if (milliseconds == null) {
    throw new RuntimeException("时间(毫秒)不能为空");
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliseconds);
    return dateToString(calendar.getTime(), pattern);
    }

    }

  • 相关阅读:
    消息中间件(一)MQ详解及四大MQ比较
    WebSocket 详解教程
    Nginx 简易教程
    排序七 归并排序
    排序五 简单选择排序
    排序四 希尔排序
    排序二 快速排序
    排序一 冒泡排序
    [算法题] 人民币大小写转换(阿拉伯数字和汉字转换)
    Linux编程 18 安装软件程序(yum工具对软件包安装,删除,更新介绍)
  • 原文地址:https://www.cnblogs.com/muliu/p/6145206.html
Copyright © 2020-2023  润新知