Java Linux时间转换失败

Java Linux时间转换失败,java,linux,Java,Linux,我的程序将以HH:MM:SS表示当前系统时间,并将其转换为秒。我把它转换成秒的原因是因为我想找出90秒前的时间例如:当前时间:12:30:3090秒前的时间:12:29:00但我无法将其设置为正确的时间戳。以下是我的程序的输出: This is the hour in sec :28800 This is the minute in sec : 1500 This is the second :0 This is the total second after minus 90 sec :3021

我的程序将以HH:MM:SS表示当前系统时间,并将其转换为秒。我把它转换成秒的原因是因为我想找出90秒前的时间

例如:
当前时间:12:30:30
90秒前的时间:12:29:00

但我无法将其设置为正确的时间戳。以下是我的程序的输出:

This is the hour in sec :28800
This is the minute in sec : 1500
This is the second :0
This is the total second after minus 90 sec :30210
The converted time after minus 90 sec :00:01:210 
当我在linux终端中发出
date+%T
命令时,它是show me
08:26:09
。我程序中显示的时间甚至与我当前的系统时间不接近。我已经将时区设置为PDT,但也无法解决问题。我的linux机器正在使用PDT时区,我不确定是不是因为时区问题。请参考下面我的代码以供参考

import java.time.LocalDateTime;
import java.text.SimpleDateFormat;
import java.util.*;

class IP
{
    public static void main(String [] args)
      {
         int hour1 = (LocalDateTime.now().getHour())*3600;
         int min1 = LocalDateTime.now().getMinute()*60;
         int sec1 = LocalDateTime.now().getSecond();
         System.out.println("This is the hour in sec :" +hour1);
         System.out.println("This is the minute in sec : "+min1);
         System.out.println("This is the second :"+sec1);
         long Tsec1 = (hour1 + min1 + sec1)-90;
         System.out.println("This is the total second after minus 90 sec :"+Tsec1);
         TimeZone tz1 = TimeZone.getTimeZone("PDT");
         SimpleDateFormat for1 = new SimpleDateFormat("HH:MM:SS");        
         for1.setTimeZone(tz1);
         System.out.println("The converted time after minus 90 sec :" +for1.format(new Date(Tsec1)));

      }

}

如果您使用的是Java 8,只需使用
LocalDateTime

如果您使用的是较低版本,请使用

不要像这样手动操纵时间,它最终会在一些你没有想到的情况下失败

编辑: 您可以像Dawood指出的那样在Java8中使用
DateTimeFormatter
。 或者可以在Java7中使用
SimpleDataFormat


要记住的是SimpleDateFormat不是线程安全的,而较新的DateTimeFormatter是线程安全的。

因为您只需要时间,所以可以使用
java.time的
LocalTime
。示例如下:

LocalTime now = LocalTime.now(ZoneId.systemDefault());
System.out.printf("Current Time: %s\n", now); // Current Time: 10:20:55.894
LocalTime ninetySecBefore = now.minusSeconds(90);
System.out.printf("Ninety Seconds earlier : %s\n", ninetySecBefore); // Ninety Seconds earlier : 10:19:25.894
System.out.println(ninetySecBefore.format(DateTimeFormatter.ofPattern("hh:mm:ss"))); //10:19:25

为什么不从
LocalDateTime
中减去90秒?可以为我提供任何相关链接吗?查看Java文档中的日期(long):。它说TSE1从时代开始需要米尔斯,而不是一天过去的几秒钟。抱歉,我在写最后一篇评论时被打断了。
LocalDateTime
类有一个
minusSeconds
方法。使用它。谢谢你的回复,我可以得到正确的时间,但我不需要日期和时区,我只需要HH:MM:SS的时间。我可以使用SimpleDataFormat来设置时间格式吗?您使用的是Java 8。您想要的类是
DateTimeFormatter
@DawoodibnKareem Spot on。但是值得指出的是
SimpleDateFormat
DateTimeFormatter
LocalTime now = LocalTime.now(ZoneId.systemDefault());
System.out.printf("Current Time: %s\n", now); // Current Time: 10:20:55.894
LocalTime ninetySecBefore = now.minusSeconds(90);
System.out.printf("Ninety Seconds earlier : %s\n", ninetySecBefore); // Ninety Seconds earlier : 10:19:25.894
System.out.println(ninetySecBefore.format(DateTimeFormatter.ofPattern("hh:mm:ss"))); //10:19:25