如何将yyy/MM/ss hh:mm:ss 转换成秒?

How to convert yyy/MM/ss hh:mm:ss into seconds?

简介

我正在尝试从两个纪元中获取秒数差异

2019-05-22 18:28:56 -> 1558542536 秒

2019-07-22 19:00:00 -> 1563814800 秒

差异为:5,272,264 秒

这个日期格式来自一个字符串形式的二进制文件

我的代码

public static void main(String[] args) throws ParseException
{
    String regEpoch = "";
    long result = 0;

    //System.out.println((fecha = dateFormat.format(date)));


    try(RandomAccessFile raf = new RandomAccessFile("binario2.txt", "rw")){
      //user inputs a code (for now, doesn't matter if exists or not)
      System.out.print("Input a code to look for: ");
        String code = scan.next();
            while(!code.matches("\d+"))
            {
                System.out.println("[ERROR] Only digits accepted");
                System.out.print("Input a code to look for: ");
                    code = scan.next();
            }


        //Gets the current date in seconds
        long getSecs = (new Date().getTime())/1000;
        System.out.println("Current tiem in secs: " + getSecs);

        //We are "randomly accessing" a binary file. The is no problem here at all. It just works.
        //Sets the pointer where I want it, again... this works fine.
        raf.seek(27+(80*Integer.parseInt(code)));

        //Read the String date correctly, which is 2019-05-22 18:28:56
        System.out.println(raf.readUTF());

        /*
        //Attempt 2
        System.out.println(java.time.Instant.ofEpochSecond(Long.parseLong(raf.readUTF())));

        Long millis = new SimpleDateFormat("yyyy/MM/ss hh:mm:ss").parse(raf.readUTF()).getTime();
        System.out.println(millis);
        */

        //Let's try to convert it into seconds... No we can't due to -> Unparseable date: "2019-05-22 18:28:56"
        Date dt = dateFormat.parse(raf.readUTF());
        long epoch = dt.getTime();
        System.out.println("Result is: " + (int)(epoch*1000));




    }catch(IOException e){System.out.println("[ERROR] " + e);} 
}

问题

我看过很多关于如何将seconds变成Epoch的问题,但是反过来呢?

到目前为止,我尝试的只是使用 SimpleDateFormat 从日期中获取秒数,但这不是我所期望的...

我对此有何期待

我目前正在做作业,我一直在计算停车罚单的价格,我想,如果汽车不离开怎么办,比方说……一周后?

如果我只按hh:mm:ss的形式工作,那些车停一整周只付一天的钱。

ChronoUnit

我总是使用 ChronoUnit 进行这样的计算。工作正常。

package test;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class Test2 {

    public static void main(String[] args) throws ParseException {

        LocalDateTime date1 = LocalDateTime.parse("2019-05-22T18:58:56");
        LocalDateTime date2 = LocalDateTime.parse("2019-05-23T19:00:00"); //LocalDateTime.now();

        long seconds = ChronoUnit.SECONDS.between(date1, date2);

        System.out.println(seconds);
    }

}

输出

86464

对于使用 SimpleDateFormat 转换为日期,您可以查看例如Java time since the epoch

这应该有效

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-05-22 18:28:56").getTime();

Duration

java.time类计算一个Duration.

调整为标准 ISO 8601 格式后解析您的输入。

LocalDateTime ldtStart = LocalDateTime.parse( "2019-05-22 18:28:56".replace( " " , "T" ) ) ;

时区

指定时区,以解决夏令时 (DST) 等异常情况。日子并不总是 24 小时。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdtStart = ldtStart.atZone( z ) ;

计算持续时间。

Duration d = Duration.between( zdtStart , zdtStop ) ;

long seconds = d.toSeconds() ;  // Or `getSeconds` before Java 9.

对于停车费,您更可能需要小时数。

long hours = d.toHours() ;