I'm having trouble with Java programming.Please let me know if you know.
画面 I would like to convert the date yyyy/MM/dd HH:mm:ss (JST) entered in the text box on the screen to UTC and UnixTime, and I would like to convert UnixTime to UTC and JST. What should I do?
テキスト I want to convert the values entered in the text box after obtaining them as follows, and display the converted values in a different text box.
text1.getText()
下記 As shown below, when I press the current time button, I try to output the time, but please tell me how to press the stop button to stop and clear the value in the text box.
if("current time".equals(cmdName)){
// "red" processing"
SwingUtilities.invokeLater(newRunnable(){
public void run() {
// Obtain UTC time from the ZonedDateTime class
ZonedDateTime utc_time
= ZoneDateTime.now(ZoneId.of("UTC"));
// Get JST time from the ZonedDateTime class
ZonedDateTime jst_time
= ZoneDateTime.now(ZoneId.of("Asia/Tokyo"));
// Get unixtime in the ZonedDateTime class
longunix_time = System.currentTimeMillis()/1000;
// Display format conversion between UTS and JST time (yyyy/mm/dd hh:mm:ss)
String utc_aft = String.format("%1$tY/%1$tm/%1$td%1$tH:%1$tM:%1$tS", utc_time);
String jst_aft = String.format("%1$tY/%1$tm/%1$td%1$tH:%1$tM:%1$tS", jst_time);
String UNIX = String.valueOf(unix_time);
text1.setText(utc_aft);
text2.setText(jst_aft);
text3.setText (UNIX);
SwingUtilities.invokeLater(this);
}
});
}
I'm sorry, but I appreciate your cooperation.
java
//import java.time.LocalDateTime;
// import java.time.ZoneId;
// import java.time.ZonedDateTime;
// import java.time.format.DateTimeFormatter;
// import javax.swing.SwingUtilities;
private boolean isStopped = true;
private void doAction (String cmdName) {
if("current time".equals(cmdName)){
isStopped=false;
// "red" processing"
SwingUtilities.invokeLater(newRunnable(){
public void run() {
if(isStopped){
text1.setText("");
text2.setText("");
text3.setText("");
return;
}
// Get JST time from the ZonedDateTime class
ZoneId JST = ZoneId.of ("Asia/Tokyo");
ZonedDateTime jst_time = ZonedDateTime.now(JST);
// Display format conversion of JST time (yyyy/mm/dd hh:mm:ss)
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String jst_aft = format.format(jst_time);
text2.setText(jst_aft);
// Get string as JST
jst_aft = text2.getText();
ZonedDateTime parsedJst = LocalDateTime.parse(jst_aft, formatter).atZone(JST);
// Obtain UTC and unixtime from JST time
ZoneDateTimeutc_time=parsedJst.withZoneSameInstant(ZoneId.of("UTC"));
longunix_time = parsedJst.toEpochSecond();
String utc_aft = format.format(utc_time);
String UNIX = String.valueOf(unix_time);
text1.setText(utc_aft);
text3.setText (UNIX);
SwingUtilities.invokeLater(this);
}
});
} else if("stop".equals(cmdName)){
isStopped = true;
}
}
DateTimeFormatter.ofPattern
The string to be given as the method argument ("yyyy/MM/dd HH:mm:ss"
in the above code) is described in the API document
Conversion from UnixTime is as follows:
You can convert to a string like format.format(utcTime)
.
画面 I would like to convert the date entered in the text box on the screen: format yyyy/MM/dd HH:mm:ss (JST) to UTC and UnixTime.
static final ZoneId UTC=ZoneId.of("UTC");
static finalZoneIdJST=ZoneId.of("Asia/Tokyo");
static final DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
// abbreviation
final String jstText=text1.getText();
finalZonedDateTime jstTime = ZonedDateTime.parse(jstText, formatter.withZone(JST));
finalZoneDateTimeutcTime=jstTime.withZoneSameInstant (UTC);
final long unixTime=utcTime.toInstant().getEpochSecond();
Also, I would like to convert UnixTime to the above format of UTC and JST.What should I do?
static final ZoneId UTC=ZoneId.of("UTC");
static finalZoneIdJST=ZoneId.of("Asia/Tokyo");
// abbreviation
final long unixTime=// omitted
finalZonedDateTimeutcTime=ZonedDateTime.from (Instant.ofEpochSecond(unixTime).atZone(UTC))
finalZoneDateTime jstTime=utcTime.withZoneSameInstant(JST);
© 2024 OneMinuteCode. All rights reserved.