package com.nokia.tsl_data.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 处理时间格式化操作
 */
public class DateFormatUtil {

    public static DateTimeFormatter ofPattern(String pattern) {
        return DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.of("Asia/Shanghai"));
    }

    public static long zeroClockTodayLong() {
        return zeroClockTodayDate().getTime();
    }

    public static Date zeroClockTodayDate() {
        String formattedDateString = new SimpleDateFormat("yyyyMMdd000000").format(new Date());
        try {
            return new SimpleDateFormat("yyyyMMddHHmmss").parse(formattedDateString);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("获取zeroClockTodayDate出错: " + e.getMessage());
        }
    }

    public static Calendar toCalendar(String formattedDateString, String parttern) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(parseDate(formattedDateString, parttern));
        return calendar;
    }

    public static Date parseDate(String formattedDateString, String parttern) {
        return parseDate(formattedDateString, new SimpleDateFormat(parttern));
    }

    public static Date parseDate(String formattedDateString, DateFormat dateFormat) {
        try {
            return dateFormat.parse(formattedDateString);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
