Guest User

Untitled

a guest
Apr 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. if("現在時刻".equals(cmdName)){
  2. // "red"の処理
  3. SwingUtilities.invokeLater(new Runnable() {
  4. public void run(){
  5.  
  6. // ZonedDateTimeクラスでUTC時間を取得
  7. ZonedDateTime utc_time
  8. = ZonedDateTime.now(ZoneId.of("UTC"));
  9.  
  10. // ZonedDateTimeクラスでJST時間を取得
  11. ZonedDateTime jst_time
  12. = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
  13.  
  14. // ZonedDateTimeクラスでunixtimeを取得
  15. long unix_time = System.currentTimeMillis()/1000;
  16.  
  17. // UTS時間とJST時間の表示形式変換(yyyy/mm/dd hh:mm:ss)
  18. String utc_aft = String.format("%1$tY/%1$tm/%1$td %1$tH:%1$tM:%1$tS",utc_time);
  19. String jst_aft = String.format("%1$tY/%1$tm/%1$td %1$tH:%1$tM:%1$tS",jst_time);
  20. String UNIX = String.valueOf(unix_time);
  21.  
  22. text1.setText(utc_aft);
  23. text2.setText(jst_aft);
  24. text3.setText(UNIX);
  25.  
  26. SwingUtilities.invokeLater(this);
  27. }
  28. });
  29. }
  30.  
  31. // import java.time.LocalDateTime;
  32. // import java.time.ZoneId;
  33. // import java.time.ZonedDateTime;
  34. // import java.time.format.DateTimeFormatter;
  35. // import javax.swing.SwingUtilities;
  36.  
  37. private boolean isStopped = true;
  38.  
  39. private void doAction(String cmdName) {
  40.  
  41. if ("現在時刻".equals(cmdName)) {
  42. isStopped = false;
  43. // "red"の処理
  44. SwingUtilities.invokeLater(new Runnable() {
  45. public void run() {
  46. if (isStopped) {
  47. text1.setText("");
  48. text2.setText("");
  49. text3.setText("");
  50. return;
  51. }
  52.  
  53. // ZonedDateTimeクラスでJST時間を取得
  54. ZoneId JST = ZoneId.of("Asia/Tokyo");
  55. ZonedDateTime jst_time = ZonedDateTime.now(JST);
  56.  
  57. // JST時間の表示形式変換(yyyy/mm/dd hh:mm:ss)
  58. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
  59. String jst_aft = formatter.format(jst_time);
  60. text2.setText(jst_aft);
  61.  
  62. // 文字列をJSTとして取得
  63. jst_aft = text2.getText();
  64. ZonedDateTime parsedJst = LocalDateTime.parse(jst_aft, formatter).atZone(JST);
  65. // JST時間からUTC時間とunixtimeを取得
  66. ZonedDateTime utc_time = parsedJst.withZoneSameInstant(ZoneId.of("UTC"));
  67. long unix_time = parsedJst.toEpochSecond();
  68.  
  69. String utc_aft = formatter.format(utc_time);
  70. String UNIX = String.valueOf(unix_time);
  71.  
  72. text1.setText(utc_aft);
  73. text3.setText(UNIX);
  74.  
  75. SwingUtilities.invokeLater(this);
  76. }
  77. });
  78. } else if ("停止".equals(cmdName)) {
  79. isStopped = true;
  80. }
  81. }
  82.  
  83. static final ZoneId UTC = ZoneId.of("UTC");
  84. static final ZoneId JST = ZoneId.of("Asia/Tokyo");
  85. static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
  86.  
  87. // 中略
  88.  
  89. final String jstText = text1.getText();
  90.  
  91. final ZonedDateTime jstTime = ZonedDateTime.parse(jstText, formatter.withZone(JST));
  92. final ZonedDateTime utcTime = jstTime.withZoneSameInstant(UTC);
  93. final long unixTime = utcTime.toInstant().getEpochSecond();
  94.  
  95. static final ZoneId UTC = ZoneId.of("UTC");
  96. static final ZoneId JST = ZoneId.of("Asia/Tokyo");
  97.  
  98. // 中略
  99.  
  100. final long unixTime = // 省略
  101.  
  102. final ZonedDateTime utcTime = ZonedDateTime.from(Instant.ofEpochSecond(unixTime).atZone(UTC))
  103. final ZonedDateTime jstTime = utcTime.withZoneSameInstant(JST);
Add Comment
Please, Sign In to add comment