package com.xty.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Dateutils {

    public static final String HH_MM_Format = "HH:mm";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";

    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static String  yearToMonth(String dateString){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

// 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat("MM-dd");
        String newDateString = newFormatter.format(date);
        return newDateString;
    }

    public static String  yearToMonth1(String dateString){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

// 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat("MM");
        String newDateString = newFormatter.format(date);
        return newDateString+"月";
    }

    public static String formatTime(long time,String format){
        DateFormat formatter = new SimpleDateFormat(format);
        Date date = new Date(time);
       return formatter.format(date);
    }

    public static String  formatTimeStr(String dateString,String format){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

// 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat(format);
        String newDateString = newFormatter.format(date);
        return newDateString;
    }
}
