Java SQL Time Create getTimeZone(String timezone, String time, DateFormat format)

Here you can find the source of getTimeZone(String timezone, String time, DateFormat format)

Description

get Time Zone

License

Open Source License

Declaration

public static String getTimeZone(String timezone, String time, DateFormat format) 

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 String getTimeZone(String timezone, String time, DateFormat format) {
        try {//from  w w w.  j a  v  a2s.  c  o  m
            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. getTimeLong(ResultSet resultSet)
  2. getTimeNumberFromSqlTime(Time time)
  3. getTimesByStr(String dataStr)
  4. getTimeStr(Time time)
  5. getTimeZone(String id)
  6. getTodayAndTime()
  7. getTomorrowOrderTime()
  8. getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date, Locale locale)
  9. getWaitTimeout(Connection con)