怎么把yyy / MM / ss hh:mm:ss转换成秒?

Whi*_*ove -1 java format date epoch 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);} 
}
Run Code Online (Sandbox Code Playgroud)

问题

我已经读过很多关于如何将秒转换为纪元的问题,但是相反呢?

  • 我必须手动进行吗?
  • 有没有我没听说过的图书馆?

So far what I tried only gets the seconds from the Date with SimpleDateFormat but those are not what I expected...

What do I expect from this

I am currently doing homework and I have been task with calculating the price for a parking ticket and I thought, what if the car doesn't leave, let's say... in a week?

If I work only in the format of hh:mm:ss those cars who stay there a whole week will only pay for one day.

hei*_*wil 5

ChronoUnit

I always use ChronoUnit for calculations like this. Works fine.

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Output

86464

For converting to date with SimpleDateFormat, you can see e.g. Java time since the epoch

  • 它只是ISO 8601标准中日期和时间之间的分隔符。参见[ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations) (2认同)