时间工具类

时间工具类


@时间封装
/**

  • 时间工具类

  • Created by MR.Z on 2020/1/6
    */
    public class DateUtil {
    public static String getCurrentDate() {
    return getFormatTime(“yyyy-MM-dd”);
    }

    public static String getCurrentTime() {
    return getFormatTime(“yyyyMMddHHmmss”);
    }

    public static String getLastYear() {
    Calendar c = Calendar.getInstance();
    Date date = new Date();
    c.setTime(date);
    int year = c.get(Calendar.DATE);
    c.set(Calendar.DATE, year + 1);
    String lastdate = new SimpleDateFormat(“yyyy-MM-dd”)
    .format(c.getTime());
    return lastdate;
    }

    public static String BackMonth(String datastring) {
    SimpleDateFormat DateFormat = new SimpleDateFormat(“yyyy-MM”);
    Date date;
    Calendar c = Calendar.getInstance();
    try {
    date = DateFormat.parse(datastring);
    c.setTime(date);
    } catch (ParseException e) {
    e.printStackTrace();
    }

     int day = c.get(Calendar.MONTH);
     c.set(Calendar.MONTH, day - 1);
     String lastdate = new SimpleDateFormat("yyyy-MM")
             .format(c.getTime());
     return lastdate;
    

    }

    public static String AddMonth(String datastring) {
    SimpleDateFormat DateFormat = new SimpleDateFormat(“yyyy-MM”);
    Date date;
    Calendar c = Calendar.getInstance();
    try {
    date = DateFormat.parse(datastring);
    c.setTime(date);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    int day = c.get(Calendar.MONTH);
    c.set(Calendar.MONTH, day + 1);
    String lastdate = new SimpleDateFormat(“yyyy-MM”)
    .format(c.getTime());
    return lastdate;
    }

    public static String getFormatTime(String format) {
    String time = null;
    Date currentDate = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    time = formatter.format(currentDate);
    return time;
    }
    }

上一篇:Object类、常用API


下一篇:java常用类