栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java时间常用工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java时间常用工具类

0.码仙励志

每个人心里都有一段伤痕,时间才是最好的疗剂。

1.常用工具类
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


public class DateUnits {

    
    public static String getThisYears() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }

    
    public static String getNextYears() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, 1);
        return sdf.format(c.getTime());
    }

    
    public static Date strToDate(String strDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date();
        }
        return date;
    }

    
    public static String getNextMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);
        return sdf.format(c.getTime());
    }


    
    public static String lastHours(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Date d = strToDate(date, "yyyy-MM-dd HH");
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.HOUR, -1);
        return sdf.format(c.getTime());
    }

    
    public static String lastDays(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = strToDate(date, "yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.DAY_OF_MONTH, -1);
        return sdf.format(c.getTime());
    }

    
    public static String lastMonth(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date d = strToDate(date, "yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.MONTH, -1);
        return sdf.format(c.getTime());
    }


    
    public static String[] dateSplit(String startDate, String endDate) {
        String[] ar = {};
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date newstartDate;
        try {
            newstartDate = simpleDateFormat.parse(startDate);
            Date newendDate = simpleDateFormat.parse(endDate);
            List dateList = new ArrayList();
            Long spi = newendDate.getTime() - newstartDate.getTime();
            
            Long step = spi / (24 * 60 * 60 * 1000);
            List newdateList = new ArrayList();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            newdateList.add(newstartDate);
            dateList.add(startDate);
            for (int i = 1; i <= step; i++) {
                dateList.add(formatter.format(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000))));
                
                newdateList.add(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000)));
            }
            String[] strings = new String[dateList.size()];
            String[] array = dateList.toArray(strings);
            return array;
        } catch (ParseException e) {
            e.printStackTrace();
            return ar;
        }
    }
    
    public static String getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year = calendar.get(Calendar.YEAR) + "";
        return "".equals(year) ? "" : year;
    }

    
    public static int getDaysOfMonth(String month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(month));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
   
    
    public static int getDaysOfMonth(String month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(month));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644384.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号