use of java.util.SimpleTimeZone in project mssql-jdbc by Microsoft.
the class UninterruptableTDSCommand method writeDateTimeOffset.
void writeDateTimeOffset(Object value, int scale, SSType destSSType) throws SQLServerException {
GregorianCalendar calendar;
// Time zone to associate with the value in the Gregorian calendar
TimeZone timeZone;
// Value to which the calendar is to be set (in milliseconds 1/1/1970 00:00:00 GMT)
long utcMillis;
int subSecondNanos;
int minutesOffset;
microsoft.sql.DateTimeOffset dtoValue = (microsoft.sql.DateTimeOffset) value;
utcMillis = dtoValue.getTimestamp().getTime();
subSecondNanos = dtoValue.getTimestamp().getNanos();
minutesOffset = dtoValue.getMinutesOffset();
// If the target data type is DATETIMEOFFSET, then use UTC for the calendar that
// will hold the value, since writeRPCDateTimeOffset expects a UTC calendar.
// Otherwise, when converting from DATETIMEOFFSET to other temporal data types,
// use a local time zone determined by the minutes offset of the value, since
// the writers for those types expect local calendars.
timeZone = (SSType.DATETIMEOFFSET == destSSType) ? UTC.timeZone : new SimpleTimeZone(minutesOffset * 60 * 1000, "");
calendar = new GregorianCalendar(timeZone, Locale.US);
calendar.setLenient(true);
calendar.clear();
calendar.setTimeInMillis(utcMillis);
writeScaledTemporal(calendar, subSecondNanos, scale, SSType.DATETIMEOFFSET);
writeShort((short) minutesOffset);
}
use of java.util.SimpleTimeZone in project mssql-jdbc by Microsoft.
the class SQLServerBulkCopy42Helper method getTemporalObjectFromCSVWithFormatter.
static Object getTemporalObjectFromCSVWithFormatter(String valueStrUntrimmed, int srcJdbcType, int srcColOrdinal, DateTimeFormatter dateTimeFormatter, SQLServerConnection connection, SQLServerBulkCopy sqlServerBC) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC42();
try {
TemporalAccessor ta = dateTimeFormatter.parse(valueStrUntrimmed);
int taHour, taMin, taSec, taYear, taMonth, taDay, taNano, taOffsetSec;
taHour = taMin = taSec = taYear = taMonth = taDay = taNano = taOffsetSec = 0;
if (ta.isSupported(ChronoField.NANO_OF_SECOND))
taNano = ta.get(ChronoField.NANO_OF_SECOND);
if (ta.isSupported(ChronoField.OFFSET_SECONDS))
taOffsetSec = ta.get(ChronoField.OFFSET_SECONDS);
if (ta.isSupported(ChronoField.HOUR_OF_DAY))
taHour = ta.get(ChronoField.HOUR_OF_DAY);
if (ta.isSupported(ChronoField.MINUTE_OF_HOUR))
taMin = ta.get(ChronoField.MINUTE_OF_HOUR);
if (ta.isSupported(ChronoField.SECOND_OF_MINUTE))
taSec = ta.get(ChronoField.SECOND_OF_MINUTE);
if (ta.isSupported(ChronoField.DAY_OF_MONTH))
taDay = ta.get(ChronoField.DAY_OF_MONTH);
if (ta.isSupported(ChronoField.MONTH_OF_YEAR))
taMonth = ta.get(ChronoField.MONTH_OF_YEAR);
if (ta.isSupported(ChronoField.YEAR))
taYear = ta.get(ChronoField.YEAR);
Calendar cal = new GregorianCalendar(new SimpleTimeZone(taOffsetSec * 1000, ""));
cal.clear();
cal.set(Calendar.HOUR_OF_DAY, taHour);
cal.set(Calendar.MINUTE, taMin);
cal.set(Calendar.SECOND, taSec);
cal.set(Calendar.DATE, taDay);
cal.set(Calendar.MONTH, taMonth - 1);
cal.set(Calendar.YEAR, taYear);
int fractionalSecondsLength = Integer.toString(taNano).length();
for (int i = 0; i < (9 - fractionalSecondsLength); i++) taNano *= 10;
Timestamp ts = new Timestamp(cal.getTimeInMillis());
ts.setNanos(taNano);
switch(srcJdbcType) {
case java.sql.Types.TIMESTAMP:
return ts;
case java.sql.Types.TIME:
// Time is returned as Timestamp to preserve nano seconds.
cal.set(connection.baseYear(), Calendar.JANUARY, 01);
ts = new java.sql.Timestamp(cal.getTimeInMillis());
ts.setNanos(taNano);
return new java.sql.Timestamp(ts.getTime());
case java.sql.Types.DATE:
return new java.sql.Date(ts.getTime());
case microsoft.sql.Types.DATETIMEOFFSET:
return DateTimeOffset.valueOf(ts, taOffsetSec / 60);
}
} catch (DateTimeException | ArithmeticException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ParsingError"));
Object[] msgArgs = { JDBCType.of(srcJdbcType) };
throw new SQLServerException(sqlServerBC, form.format(msgArgs), null, 0, false);
}
// unreachable code. Need to do to compile from Eclipse.
return valueStrUntrimmed;
}
use of java.util.SimpleTimeZone in project h2database by h2database.
the class TestBase method assertEquals.
/**
* Check if two values are equal, and if not throw an exception.
*
* @param expected the expected value
* @param actual the actual value
* @throws AssertionError if the values are not equal
*/
public void assertEquals(java.util.Date expected, java.util.Date actual) {
if (!Objects.equals(expected, actual)) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleTimeZone gmt = new SimpleTimeZone(0, "Z");
df.setTimeZone(gmt);
fail("Expected: " + (expected != null ? df.format(expected) : "null") + " actual: " + (actual != null ? df.format(actual) : "null"));
}
}
use of java.util.SimpleTimeZone in project AJSC by att.
the class Time method getDateFormatter.
/**
* Returns singleton UTC date formatter.
*
* @return The date formatter for UTC dates and times
*/
@SuppressWarnings("nls")
private static SimpleDateFormat getDateFormatter() {
if (dateformatter == null) {
dateformatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateformatter.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
}
return dateformatter;
}
use of java.util.SimpleTimeZone in project aliyun-oss-java-sdk by aliyun.
the class BucketOperationsSample method formatISO8601Date.
private static String formatISO8601Date(Date date) {
final String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.US);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
return dateFormat.format(date);
}
Aggregations