package com.dxhy.passengerticket.util;

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

/**
 * @author xiexuedi
 * @date 2019/05/08 16:51:22 用于获取月份结束的时间戳
 */
public class MonthTimestamp {

    private static final ThreadLocal<DateFormat> THREAD_LOCAL =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    public static String motnthStart(String reportingPeriod) {
        int year = Integer.parseInt(reportingPeriod.substring(0, 4));
        int month = Integer.parseInt(reportingPeriod.substring(reportingPeriod.length() - 2));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return THREAD_LOCAL.get().format(calendar.getTime());
    }

    public static String motnthEnd(String reportingPeriod) {
        int year = Integer.parseInt(reportingPeriod.substring(0, 4));
        int month = Integer.parseInt(reportingPeriod.substring(reportingPeriod.length() - 2));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return THREAD_LOCAL.get().format(calendar.getTime());
    }

}
