Java SQL Time Create getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date, Locale locale)

Here you can find the source of getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date, Locale locale)

Description

Gets the userToServerDateTime attribute of the DateUtils class

License

Open Source License

Parameter

Parameter Description
timeZone Description of the Parameter
dateFormat Description of the Parameter
timeFormat Description of the Parameter
date Description of the Parameter
locale Description of the Parameter

Return

The userToServerDateTime value

Declaration

public static Timestamp getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date,
        Locale locale) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**//from ww w.  j  a  va 2 s .co m
     *  Gets the userToServerDateTime attribute of the DateUtils class
     *
     * @param  timeZone    Description of the Parameter
     * @param  dateFormat  Description of the Parameter
     * @param  timeFormat  Description of the Parameter
     * @param  date        Description of the Parameter
     * @param  locale      Description of the Parameter
     * @return             The userToServerDateTime value
     */
    public static Timestamp getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date,
            Locale locale) {
        try {
            DateFormat localeFormatter = DateFormat.getDateInstance(dateFormat, locale);
            if (timeZone != null) {
                localeFormatter.setTimeZone(timeZone);
            }
            localeFormatter.setLenient(false);
            return new Timestamp(localeFormatter.parse(date).getTime());
        } catch (Exception e) {
            if (date != null && !"".equals(date)) {
                System.err.println("EXCEPTION: DateUtils-> Timestamp " + e);
            }
        }
        return null;
    }

    /**
     *  Returns the converted server time based on the current calendar time and
     *  timezone of the user
     *
     * @param  cal       Description of the Parameter
     * @param  timeZone  Description of the Parameter
     * @return           The userToServerDateTime value
     */
    public static java.sql.Timestamp getUserToServerDateTime(Calendar cal, TimeZone timeZone) {
        java.sql.Timestamp timestampValue = null;
        try {
            String date = getDateString(cal);
            DateFormat localFormatter = DateFormat.getDateInstance(DateFormat.SHORT);
            if (timeZone != null) {
                localFormatter.setTimeZone(timeZone);
            }
            timestampValue = new java.sql.Timestamp(localFormatter.parse(date).getTime());
        } catch (Exception e) {
            System.out.println("DateUtils-> getUserToServerDateTime Exception" + e.toString());
        }
        return timestampValue;
    }

    /**
     *  Returns the current date of the calendar in the m/d/yyyy format as a
     *  string
     *
     * @param  cal  Description of the Parameter
     * @return      The dateString value
     */
    public static String getDateString(Calendar cal) {
        return (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR);
    }
}

Related

  1. getTimeStr(Time time)
  2. getTimeZone(String id)
  3. getTimeZone(String timezone, String time, DateFormat format)
  4. getTodayAndTime()
  5. getTomorrowOrderTime()
  6. getWaitTimeout(Connection con)
  7. getYear(String dateTime)
  8. getYear(String dateTime)
  9. substract(Time thisDeparture, Time firstDeparture)