package cn.gintone.iotdbUtils;

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


public class MyDateUtils {
    public static long stringToLong(String timeStr){
        try {
            boolean hasMillis = timeStr.contains(".");
            String pattern = hasMillis ? "yyyy-MM-dd HH:mm:ss.SSS" : "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(timeStr);
            return date.getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String longToString(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date(timestamp);
        return sdf.format(date);
    }
}
