package com.reptileclient.jlifeapp.util;

import cn.hutool.core.date.DateUtil;

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

public class DateUtils {
	public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	
	public static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	
	public static String getCurrentDay() {
		return format.format(new Date());
	}
	
	/**
	 * 获取前一天
	 * 
	 * @param date 当前日期
	 * @return
	 */
	public static String getFrontDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, -1);

		return format.format(c.getTime());
	}

	/**
	 * 获取当前月第一天
	 * 
	 * @return
	 */
	public static String getMonthFirstDay() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = format.format(c.getTime());
		return first;
	}

	/**
	 * 获取当月的最后一天
	 * 
	 * @return
	 */
	public static String getMonthEndDay() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = format.format(ca.getTime());
		return last;
	}

	/**
	 * 获取前一天的月份，的第一天
	 */
	public static String qytdyddyt() {
		String frontDay = getFrontDay(new Date());
		String substring = frontDay.substring(5, 7);
		String y=frontDay.substring(0, 4);

		Calendar c = Calendar.getInstance();

		c.set(Calendar.YEAR, Integer.parseInt(y));
		c.set(Calendar.MONTH, Integer.parseInt(substring) - 1);

		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = format.format(c.getTime());
		return first;
	}
	
	/**
	 * 获取前一天的月份的最后一天
	 * @return
	 */
	public static String qytdydzhyt() {
		String frontDay = getFrontDay(new Date());
		String substring = frontDay.substring(5, 7);
		String y=frontDay.substring(0, 4);

		Calendar c = Calendar.getInstance();

		c.set(Calendar.YEAR, Integer.parseInt(y));
		c.set(Calendar.MONTH, Integer.parseInt(substring) - 1);

		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = format.format(c.getTime());
		return last;
	}
	

	public static void main(String[] args) {
		System.out.println(DateUtil.lastMonth());
	}

}
