编辑代码

import java.util.ArrayList;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

class Main {
public static void main(String[] args) {
        //System.out.println("-------------------- 2019-09-24 00:00:00 -----------------------");
        //System.out.println("local: "+getTimeZoneTimeStr("2019-09-24 00:00:00",null));
        System.out.println("Asia/Shanghai: "+getTimeZoneTimeStr("2021-08-13 11:57:10","Asia/Shanghai"));
        System.out.println("GMT+0200: "+getTimeZoneTimeStr("2021-08-13 05:57:10","GMT+0200"));
        //System.out.println("GMT: "+getTimeZoneTimeStr("2019-09-24 00:00:00","GMT"));
        //System.out.println("UTC: "+getTimeZoneTimeStr("2019-09-24 00:00:00","UTC"));

        //System.out.println("-------------------- 2019-09-23 16:00:00 -----------------------");
        //System.out.println("local: "+getTimeZoneTimeStr("2019-09-23 16:00:00",null));
        //System.out.println("Asia/Shanghai: "+getTimeZoneTimeStr("2019-09-23 16:00:00","Asia/Shanghai"));
        //System.out.println("GMT+0800: "+getTimeZoneTimeStr("2019-09-23 16:00:00","GMT+0800"));
        //System.out.println("GMT: "+getTimeZoneTimeStr("2019-09-23 16:00:00","GMT"));
        //System.out.println("UTC: "+getTimeZoneTimeStr("2019-09-23 16:00:00","UTC"));
        //System.out.println(new Date().getTime());
    }


public static long getTimeZoneTimeStr(String dateStr,String timeZone) {
        long result = 0L;
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
        int millisecond;
        Calendar calendarTime = Calendar.getInstance();

        if(timeZone != null){
            TimeZone tz = TimeZone.getTimeZone(timeZone);
            calendarTime.setTimeZone(tz);
        }

        if (null != dateStr && 14 == dateStr.length()) {
            year = Integer.parseInt(dateStr.substring(0, 4));
            month = Integer.parseInt(dateStr.substring(4, 6));
            day = Integer.parseInt(dateStr.substring(6, 8));
            hour = Integer.parseInt(dateStr.substring(8, 10));
            minute = Integer.parseInt(dateStr.substring(10, 12));
            second = Integer.parseInt(dateStr.substring(12, 14));
            calendarTime.set(1, year);
            calendarTime.set(2, month - 1);
            calendarTime.set(5, day);
            calendarTime.set(11, hour);
            calendarTime.set(12, minute);
            calendarTime.set(13, second);
            result = calendarTime.getTime().getTime();
        }else if (null != dateStr && 19 == dateStr.length()) {
            year = Integer.parseInt(dateStr.substring(0, 4));
            month = Integer.parseInt(dateStr.substring(5, 7));
            day = Integer.parseInt(dateStr.substring(8, 10));
            hour = Integer.parseInt(dateStr.substring(11, 13));
            minute = Integer.parseInt(dateStr.substring(14, 16));
            second = Integer.parseInt(dateStr.substring(17, 19));
            calendarTime.set(1, year);
            calendarTime.set(2, month - 1);
            calendarTime.set(5, day);
            calendarTime.set(11, hour);
            calendarTime.set(12, minute);
            calendarTime.set(13, second);
            result = calendarTime.getTime().getTime();
        }else if (null != dateStr && 23 == dateStr.length()){
            year = Integer.parseInt(dateStr.substring(0, 4));
            month = Integer.parseInt(dateStr.substring(5, 7));
            day = Integer.parseInt(dateStr.substring(8, 10));
            hour = Integer.parseInt(dateStr.substring(11, 13));
            minute = Integer.parseInt(dateStr.substring(14, 16));
            second = Integer.parseInt(dateStr.substring(17, 19));
            millisecond = Integer.parseInt(dateStr.substring(20, 23));
            calendarTime.set(1, year);
            calendarTime.set(2, month - 1);
            calendarTime.set(5, day);
            calendarTime.set(11, hour);
            calendarTime.set(12, minute);
            calendarTime.set(13, second);
            calendarTime.set(14, millisecond);
            result = calendarTime.getTime().getTime();
        }

        return result;
    }    
}