zoukankan      html  css  js  c++  java
  • 时间类的操作

    package com.aspire.prnp.util;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    import java.util.TreeMap;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class DataFormatUtil {

    private static Logger logger = LoggerFactory.getLogger(DataFormatUtil.class);

    public static final String dataFormat = "yyyyMMdd";

    public static final String timeFormat = "yyyyMMddHHmmss";

    public static final String timeFormatForView = "yyyyMMdd HH:mm:ss";

    public static final String DATA_FORMAT_1 = "yyyy-MM-dd";

    public static final String DATA_FORMAT_2 = "yyyy年MM月dd日";

    public static final String DATA_FORMAT_3 = "yyyy-MM";

    /**
    * 取当前时间,格式:yyyyMMddHHmmss
    *
    * @return yyyyMMddHHmmss
    */
    public static String getCurrentTime() {
    Date date = new Date();
    SimpleDateFormat formatterLong = new SimpleDateFormat("yyyyMMddHHmmss");
    return formatterLong.format(date);
    }

    /**
    * 取当前时间,格式:yyyy-MM-dd HH:mm:ss
    *
    * @return yyyy-MM-dd HH:mm:ss
    */
    public static String getCurrentFormateTime() {
    Date date = new Date();
    SimpleDateFormat standart_formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return standart_formatter.format(date);
    }

    public static String getDataString(String format, Date date){
    SimpleDateFormat fmt=new SimpleDateFormat(format);
    return fmt.format(date);
    }

    public static Date getData(String format, String date){
    SimpleDateFormat fmt=new SimpleDateFormat(format);
    fmt.setLenient(false);
    try {
    return fmt.parse(date);
    } catch (ParseException e) {
    logger.error("时间转换异常", e);
    }
    return null;
    }

    /**
    * 日期型字符串转换
    * @param inFormat
    * @param outFotmat
    * @param time
    * @return
    */
    public static String formatDate(String inFormat, String outFotmat, String time) {

    DateFormat sdf = new SimpleDateFormat(inFormat);
    DateFormat sdf1 = new SimpleDateFormat(outFotmat);

    try {

    Date date = sdf.parse(time);
    return sdf1.format(date);
    } catch (ParseException e) {

    logger.info("转换日期异常", e);
    }

    return null;
    }

    /**
    * 将日期按季度划分返回
    * @param startMonth 开始日期
    * @param endMonth 结束日期
    * @return
    */
    public static List<String> queryQuater(String startMonth, String endMonth) {
    List<String> listDate = new ArrayList<String>();
    Map<String, String> map = new HashMap<String, String>();
    // 得出所有得月份
    String[] st = getMonthArray(startMonth, endMonth);
    for (String s : st) {
    String yo = s.substring(0, 4);
    String mo = s.substring(5, 7);
    if ("01".equals(mo) || "02".equals(mo) || "03".equals(mo)) {
    map.put(yo + "1", yo + "年1季度");
    } else if ("04".equals(mo) || "05".equals(mo) || "06".equals(mo)) {
    map.put(yo + "2", yo + "年2季度");
    } else if ("07".equals(mo) || "08".equals(mo) || "09".equals(mo)) {
    map.put(yo + "3", yo + "年3季度");
    } else if ("10".equals(mo) || "11".equals(mo) || "12".equals(mo)) {
    map.put(yo + "4", yo + "年4季度");
    }
    }
    Map<String, String> resultMap = sortMapByKey(map);
    Set<?> set = resultMap.entrySet();
    Iterator<?> iterator = set.iterator();
    while (iterator.hasNext()) {
    @SuppressWarnings("unchecked")
    Map.Entry<String, Object> enter = (Entry<String, Object>) iterator.next();
    listDate.add(enter.getValue().toString());
    }

    return listDate;
    }
    /**
    *
    * method_name:getDayInMonthArray
    * date:2017年6月20日上午10:44:46
    * author:suzihua
    * return_type:String[]
    * description:根据当前日期获取当月每天的日期(不包含当天)
    */
    public static String[] getDayInMonthArray(){
    // 定义集合存放日期
    List<String> list = new ArrayList<String>();
    // 获取当月第一天和最后一天
    SimpleDateFormat format = new SimpleDateFormat(DATA_FORMAT_1);

    Date currDate = new Date();
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.MONTH, 0);
    cale.set(Calendar.DAY_OF_MONTH, 1);
    Date firtsDay = cale.getTime();

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(firtsDay);
    c2.setTime(currDate);
    c2.add(Calendar.DAY_OF_MONTH, -1);//不包含当天
    list.add(format.format(firtsDay));
    while(c1.compareTo(c2) < 0){
    c1.add(Calendar.DAY_OF_MONTH, 1);
    Date date = c1.getTime();
    list.add(format.format(date));
    }
    // 存放入数组
    String[] resultArray = new String[list.size() - 1];
    resultArray = list.toArray(new String[]{});
    return resultArray;
    }
    /**
    *
    * method_name:getMonthInYearArray
    * date:2017年6月20日下午1:36:56
    * author:suzihua
    * return_type:String[]
    * description:根据当前日期获取当年每月的日期
    */
    public static String[] getMonthInYearArray(){
    // 定义集合存放月份
    List<String> list = new ArrayList<String>();
    // 获取当月第一天和最后一天
    SimpleDateFormat format = new SimpleDateFormat(DATA_FORMAT_3);

    Date currDate = new Date();
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.YEAR, 0);
    cale.set(Calendar.DAY_OF_YEAR, 1);
    Date firtsDay = cale.getTime();

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(firtsDay);
    c2.setTime(currDate);
    c2.set(Calendar.DAY_OF_MONTH, 1);
    list.add(format.format(firtsDay));
    while(c1.compareTo(c2) < 0){
    c1.add(Calendar.MONTH, 1);
    Date date = c1.getTime();
    list.add(format.format(date));
    }
    // 存放入数组
    String[] resultArray = new String[list.size() - 1];
    resultArray = list.toArray(new String[]{});
    return resultArray;
    }
    /**
    *
    * method_name:getYearArray
    * date:2017年6月21日下午4:58:27
    * author:suzihua
    * return_type:String[]
    * description:获取指定日期之后的年份 直到当前年份
    */
    public static String[] getYearArray(int year){

    Calendar c = Calendar.getInstance();
    int y = c.get(Calendar.YEAR);
    int num = y - year+1;
    String[] result = new String[num];
    for(int i=0;i<num;i++){
    result[i]=String.valueOf(year);
    year++;
    }
    return result;
    }

    public static String[] getMonthArray(String startMonth, String endMonth) {
    DateFormat df = DateFormat.getDateInstance();
    Date date1 = null; // 开始日期
    Date date2 = null; // 结束日期
    try {
    date1 = df.parse(startMonth);
    date2 = df.parse(endMonth);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    // 定义集合存放月份
    List<String> list = new ArrayList<String>();
    // 添加第一个月,即开始时间
    list.add(startMonth.substring(0, 7));
    c1.setTime(date1);
    c2.setTime(date2);
    while (c1.compareTo(c2) < 0) {
    c1.add(Calendar.MONTH, 1);// 开始日期加一个月直到等于结束日期为止
    Date ss = c1.getTime();
    String str = df.format(ss);
    str = str.substring(0, str.lastIndexOf("-"));
    list.add(str);
    }
    // 存放入数组
    String[] resultArray = new String[list.size() - 1];
    for (int i = 0; i < list.size() - 1; i++) {
    if (list.get(i).length() == 6) {
    resultArray[i] = (String) list.get(i).replace("-", "-0");
    } else {
    resultArray[i] = (String) list.get(i);
    }

    }
    return resultArray;
    }

    /**
    * 使用 Map按key进行排序
    *
    * @param map
    * @return
    */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
    if (map == null || map.isEmpty()) {
    return null;
    }
    Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
    sortMap.putAll(map);
    return sortMap;
    }
    /**
    *
    * method_name:getDatesBetweenTwoDate
    * date:2017年4月26日上午10:39:41
    * author:suzihua
    * return_type:List<Date>
    * description:
    */
    public static List<String> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
    List<String> lDate = new ArrayList<String>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String startTime = sdf.format(beginDate);
    lDate.add(startTime);// 把开始时间加入集合
    Calendar cal = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
    cal.setTime(beginDate);
    boolean bContinue = true;
    while (bContinue) {
    // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
    cal.add(Calendar.DAY_OF_MONTH, 1);
    // 测试此日期是否在指定日期之后
    if (endDate.after(cal.getTime())) {
    Date date = cal.getTime();
    lDate.add(sdf.format(date));
    } else {
    break;
    }
    }
    lDate.add(sdf.format(endDate));// 把结束时间加入集合
    return lDate;
    }

    }

    class MapKeyComparator implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {

    return str1.compareTo(str2);
    }
    }

  • 相关阅读:
    文件操作:根据现有类生成所需要的类
    Microsoft JScript 运行时错误: “”未定义
    未完成
    WPF模拟雷达界面效果图
    WebClient模拟网页提交表单
    201319
    Delphi中的InStrRev函数(倒找文本)
    利用IDhttp实现图片下载
    白话解释哈希表
    整理的Delphi常用字符串函数
  • 原文地址:https://www.cnblogs.com/fuqiang-terry/p/7120214.html
Copyright © 2011-2022 走看看