Java SQL Date From getCNTimezone()

Here you can find the source of getCNTimezone()

Description

get CN Timezone

License

Open Source License

Declaration

public static TimeZone getCNTimezone() 

Method Source Code


//package com.java2s;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;

import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static TimeZone getCNTimezone() {
        return TimeZone.getTimeZone("Asia/Shanghai");
    }/*from  ww  w  .ja va  2  s . c  o m*/

    public static String getTimeZone(String timezone, String time, DateFormat format) {
        try {
            time = time.trim();
            if (time.endsWith(":00")) {
                time = time.substring(0, time.length() - 3) + "00";
            }
            Date date = parseDateStringToDate(format, time);
            Calendar ca = Calendar.getInstance();
            ca.clear();
            ca.setTimeInMillis(date.getTime());
            format.setTimeZone(TimeZone.getTimeZone(timezone));
            time = Format(format, ca.getTime());
            format.setTimeZone(TimeZone.getDefault());
        } catch (Exception e) {
        }
        return time;
    }

    public static Date parseDateStringToDate(DateFormat format, String dateString) {
        if (format != null) {
            synchronized (format) {
                try {
                    return format.parse(dateString);
                } catch (ParseException e) {
                    return null;
                }
            }
        } else {
            // String message = "format is null,the dataString is " +
            // dateString;
            return null;
        }

    }

    public static Date parseDateStringToDate(DateFormat format, String time, TimeZone timeZone) {
        synchronized (format) {
            try {
                format.setTimeZone(timeZone);
                return format.parse(time);
            } catch (ParseException e) {
                return null;
            }
        }
    }

    public static String Format(DateFormat format, Timestamp time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time);
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }

    public static String Format(DateFormat format, Date time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time).replace("24:", "00:");
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }

    public static String Format(DateFormat format, long time) {
        synchronized (format) {
            String t = "";
            try {
                t = format.format(time);
            } catch (Exception e) {
                return t;
            }
            return t;
        }
    }
}

Related

  1. convertTime2TS(long time)
  2. convertTimeToHour(long time)
  3. convertToDateTime(java.util.Date date, String time)
  4. convertToTime(Date toDate)
  5. convertToTime(java.util.Date date)
  6. getCurAllTime()
  7. getCurrDateTime()
  8. getCurrentDateTime()
  9. getCurrentTime()