zoukankan      html  css  js  c++  java
  • DateUtil

    package com.mz.base.util;

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

    import org.apache.commons.lang3.StringUtils;

    /**
    * 日期时间帮助类
    * @author zejun
    */
    public class DateUtil {
    /**
    * 默认格式串
    */
    private static final String FORMA_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    /**
    * 获取当前时间
    * @return
    */
    public static Date getDate(){
    return new Date();
    }
    /**
    * 将时间戳转换成date
    * @param timestamp
    * @return
    */
    public static Date getDate(Long timestamp){
    return new Date(timestamp);
    }
    /**
    * 返回yyyy-mm-dd HH:mm:ss格式的日期字符串
    * @return
    */
    public static String formaDate(){
    SimpleDateFormat format = new SimpleDateFormat(FORMA_DEFAULT);
    return format.format(new Date());
    }
    /**
    * 将时间戳转换成date,并返回yyyy-mm-dd HH:mm:ss格式的日期字符串
    * @return
    */
    public static String formaDate(Long timestamp){
    SimpleDateFormat format = new SimpleDateFormat(FORMA_DEFAULT);
    return format.format(new Date(timestamp));
    }
    /**
    * 格式化时间
    * @param date
    * @return
    */
    public static String formatDate(Date date){
    SimpleDateFormat format = new SimpleDateFormat(FORMA_DEFAULT);
    return format.format(date);
    }
    /**
    * 格式化时间
    * @param date
    * @param formatStr
    * @return
    */
    public static String formatDate(Date date, String formatStr){
    if(StringUtils.isEmpty(formatStr)){
    formatStr = FORMA_DEFAULT;
    }
    SimpleDateFormat format = new SimpleDateFormat(formatStr);
    return format.format(date);
    }
    /**
    * 格式化时间
    * @param date
    * @param formatStr
    * @return
    */
    public static String formatDate(String formatStr){
    if(StringUtils.isEmpty(formatStr)){
    formatStr = FORMA_DEFAULT;
    }
    SimpleDateFormat format = new SimpleDateFormat(formatStr);
    return format.format(new Date());
    }
    /**
    * 格式化时间
    * @param date
    * @param formatStr
    * @return
    */
    public static String formatDate(Long timestamp, String formatStr){
    if(StringUtils.isEmpty(formatStr)){
    formatStr = FORMA_DEFAULT;
    }
    SimpleDateFormat format = new SimpleDateFormat(formatStr);
    return format.format(new Date(timestamp));
    }
    /**
    * 获得当前-年
    * @return
    */
    public static int getYear(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.YEAR);
    }
    /**
    * 获得当前-月
    * @return
    */
    public static int getMonth(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.MONTH) + 1;
    }
    /**
    * 获得当前-日
    * @return
    */
    public static int getDay(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.DATE);
    }
    /**
    * 获得当前-时(12小时制)
    * @return
    */
    public static int getHour(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.HOUR);
    }
    /**
    * 获得当前-时(24小时制)
    * @return
    */
    public static int getHour24(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.HOUR_OF_DAY);
    }
    /**
    * 获得当前-分
    * @return
    */
    public static int getMinute(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.MINUTE);
    }
    /**
    * 获得当前-秒
    * @return
    */
    public static int getSecond(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.SECOND);
    }
    /**
    * 获得当前-星期
    * 返回整型 1|星期日; 2|星期一; 3|星期二 ; 4|星期三; ......
    * @return
    */
    public static int getWeek(){
    Calendar c = Calendar.getInstance();
    return c.get(Calendar.DAY_OF_WEEK);
    }
    /**
    * 获得当前-星期
    * 返回整型 1|星期日; 2|星期一; 3|星期二 ; 4|星期三; ......
    * @return
    */
    public static String getWeekToString(){
    String[] weekStr = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    Calendar c = Calendar.getInstance();
    int week = c.get(Calendar.DAY_OF_WEEK) - 1;
    return weekStr[week];
    }
    }

  • 相关阅读:
    背水一战 Windows 10 (61)
    背水一战 Windows 10 (60)
    背水一战 Windows 10 (59)
    背水一战 Windows 10 (58)
    背水一战 Windows 10 (57)
    背水一战 Windows 10 (56)
    背水一战 Windows 10 (55)
    背水一战 Windows 10 (54)
    背水一战 Windows 10 (53)
    背水一战 Windows 10 (52)
  • 原文地址:https://www.cnblogs.com/yufeng1102/p/7536295.html
Copyright © 2011-2022 走看看