zoukankan      html  css  js  c++  java
  • 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);
    }

    }

  • 相关阅读:
    修改sql表操作大全
    Asp.NET自定义DataGrid控件
    jQuery LigerUI 插件介绍及使用之ligerGrid
    jQuery LigerUI API预览版发布
    jQuery LigerUI 初次发布&一睹为快(提供Demo下载)
    jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable
    nginx 日志
    nginx 正向代理
    排序
    nginx 静态资源优化
  • 原文地址:https://www.cnblogs.com/muliu/p/6145206.html
Copyright © 2011-2022 走看看