import java.sql.Timestamp; //导入方法依赖的package包/类

/**

* Tests fix for BUG#5874, timezone correction goes in wrong 'direction' (when useTimezone=true and server timezone differs from client timezone).

*

* @throws Exception

* if the test fails.

*/

public void testBug5874() throws Exception {

TimeZone defaultTimezone = TimeZone.getDefault();

try {

String clientTimezoneName = "America/Los_Angeles";

String serverTimezoneName = "America/Chicago";

TimeZone.setDefault(TimeZone.getTimeZone(clientTimezoneName));

long clientTimezoneOffsetMillis = TimeZone.getDefault().getRawOffset();

long serverTimezoneOffsetMillis = TimeZone.getTimeZone(serverTimezoneName).getRawOffset();

long offsetDifference = clientTimezoneOffsetMillis - serverTimezoneOffsetMillis;

SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

long pointInTime = timestampFormat.parse("2004-10-04 09:19:00").getTime();

Properties props = new Properties();

props.put("useTimezone", "true");

props.put("serverTimezone", serverTimezoneName);

props.put("cacheDefaultTimezone", "false");

Connection tzConn = getConnectionWithProps(props);

Statement tzStmt = tzConn.createStatement();

createTable("testBug5874", "(tstamp DATETIME, t TIME)");

PreparedStatement tsPstmt = tzConn.prepareStatement("INSERT INTO testBug5874 VALUES (?, ?)");

tsPstmt.setTimestamp(1, new Timestamp(pointInTime));

tsPstmt.setTime(2, new Time(pointInTime));

tsPstmt.executeUpdate();

this.rs = tzStmt.executeQuery("SELECT * from testBug5874");

while (this.rs.next()) { // Driver now converts/checks DATE/TIME/TIMESTAMP/DATETIME types when calling getString()...

String retrTimestampString = new String(this.rs.getBytes(1));

Timestamp retrTimestamp = this.rs.getTimestamp(1);

java.util.Date timestampOnServer = timestampFormat.parse(retrTimestampString);

long retrievedOffsetForTimestamp = retrTimestamp.getTime() - timestampOnServer.getTime();

assertEquals("Original timestamp and timestamp retrieved using client timezone are not the same", offsetDifference,

retrievedOffsetForTimestamp);

String retrTimeString = new String(this.rs.getBytes(2));

Time retrTime = this.rs.getTime(2);

java.util.Date timeOnServerAsDate = timeFormat.parse(retrTimeString);

Time timeOnServer = new Time(timeOnServerAsDate.getTime());

long retrievedOffsetForTime = retrTime.getTime() - timeOnServer.getTime();

assertEquals("Original time and time retrieved using client timezone are not the same", offsetDifference, retrievedOffsetForTime);

}

tzConn.close();

} finally {

TimeZone.setDefault(defaultTimezone);

}

}

java get timestamp_Java Timestamp.getTime方法代码示例相关推荐

  1. cdate在java中_Java Calendar.add方法代码示例

    本文整理汇总了Java中java.util.Calendar.add方法的典型用法代码示例.如果您正苦于以下问题:Java Calendar.add方法的具体用法?Java Calendar.add怎 ...

  2. java hashmap putall_Java ConcurrentHashMap.putAll方法代码示例

    import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类 /** * 生成更新Sql * @param entity * @ ...

  3. java中setattribute_Java Files.setAttribute方法代码示例

    import java.nio.file.Files; //导入方法依赖的package包/类 @Override public void setFileHiddenAttribute( String ...

  4. java gettime_Java Util.getTime方法代码示例

    import org.jrobin.core.Util; //导入方法依赖的package包/类 private void initGraphPeriodAndSize(Range range, in ...

  5. java中setDocument_Java ActionItem.setDocumentId方法代码示例

    import org.kuali.rice.kew.actionitem.ActionItem; //导入方法依赖的package包/类 @Test public void testConvertAc ...

  6. java sql xml_Java ResultSet.getSQLXML方法代码示例

    import java.sql.ResultSet; //导入方法依赖的package包/类 protected Object processColumn(ResultSet rs, int inde ...

  7. java中mymaps_Java MyLocationStyle.myLocationType方法代码示例

    import com.amap.api.maps.model.MyLocationStyle; //导入方法依赖的package包/类 @Override protected void onCreat ...

  8. java default locale_Java JSON.defaultLocale方法代码示例

    import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类 public void test_time() throws Exception { lon ...

  9. java中settext_Java JCheckBoxMenuItem.setText方法代码示例

    import javax.swing.JCheckBoxMenuItem; //导入方法依赖的package包/类 private void reloadLang() { lang = JLang.g ...

最新文章

  1. c语言实现将字符串首尾*删除,C语言实现Trim()函数:删除字符串首尾空格。...
  2. Rabbitmq如何保证消息顺序执行
  3. 计算机考试单元格,计算机等级考试技巧在Excel中如何对单元格进行设置?
  4. javascript-排序算法
  5. linux实现自动互信,Linux 使用shell脚本实现自动SSH互信功能
  6. servlet中的数据存储
  7. cocos2dx 3.0研究(1)-- hello world程序
  8. Intellij IDEA 神级插件!效率提升 10 倍!
  9. opencv 滤镜效果php,opencv滤镜-去色滤镜
  10. 屏蔽广告方法(Adblock 插件)
  11. 音频编码格式——AAC简介
  12. 易基因 | 国自然选题——易基因RNA甲基化测序技术(m6A m5C)正热门
  13. 附录一 常见概率分布及相关公式
  14. 学习html+css+js笔记
  15. 硬改路由器-MW310R-AR9341篇
  16. FIFO的发送条件总结
  17. 星座图中格雷映射及其实现
  18. Upload to server password failed. Failed to transfer file . Permission denied.
  19. 学位计算机知识点,学位计算机考试归纳大全.doc
  20. SuperMap iClient3D for WebGL沿线飞行飞行(二)

热门文章

  1. python爬取淘宝搜索页面+url+图片下载并将信息保存到MySQL数据库中
  2. Python3安装PyYAML
  3. 函数组:SCMS_CONV
  4. 计算机算法基础_考情分析|2020年华南理工大学计算机考研考情分析
  5. ie不能加载flash html,IE浏览器无法显示Flash怎么解决
  6. CMIP6数据多种方式下载——IDM(Windows),wget,acccmip6(Linux)
  7. 类似微信表情图文混排(本地自定义表情)
  8. stame进去显示服务器,steam显示更新服务器
  9. 4399玩过的服务器找不到了,4399小游戏:曾经玩到忘记吃饭了,为何如今有时间也不玩了?...
  10. 蓝牙HID——Android手机注册HID时出现 Could not bind to Bluetooth (HID Device) Service with Intent * 的问题分析