日历减去日期问题

时间:2017-02-01 00:06:21

标签: java date parsing calendar printwriter

我有一个差不多完成的剧本,但有一个轻微的打嗝,我似乎无法修复。

首先让我快速解释一下脚本。它使用Calendar获取当前日期。然后它将当前日期添加2天,之后创建格式,以更改新日期的格式。然后,它将该新日期打印到.txt文件,然后使用Scanner从.txt文件中获取该日期。就在这里,我有点麻烦。我从.txt文件获得的新日期,我想从实际当前日期中减去。因此,我可以从当前系统日期倒计时,直到2天。目前,代码使用减法/倒计时函数中的日期变量todAdd进行减法,该函数是代码的最后6行。但是它不是那个日期变量,我希望它是dateFroText变量,但我无法将其解析为最后一个减法代码,因为它似乎没有处理解析变化很好。

所以基本上举一个例子我希望整个倒计时部分让todAdd改为dateFroText。因此,不是您在下面的完整代码中看到的行,而是像int yearDiff = dateFroText.get(Calendar.YEAR) - startDat.get(Calendar.YEAR);

目前我已尝试将其更改为integerstringdate,但所有这些都会产生一个错误,导致无法运行代码。

如果有必要,我已经复制了整个代码,因此您可以对其进行实际测试。

public class CdowTDate
{

    static File dFileD = new File("date.txt");
    static String date = "";

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

      //Get current date and time
      Calendar startDat = Calendar.getInstance();
      System.out.println("Current date: " + startDat.getTime());

      //Get that current date and time and then add 2 days
      Calendar todAdd = Calendar.getInstance();
      todAdd.add(Calendar.DATE, 2);
      System.out.println("Date in 2 days: " + todAdd.getTime());

      //Create a format for sending date to text file
      SimpleDateFormat formDat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
      String formatted = formDat.format(todAdd.getTime());
      System.out.println("Formatted: " + formatted);

      try{
          PrintWriter dW = new PrintWriter("date.txt");
          dW.println(formatted);
          dW.close();
      } catch (IOException e) {
      }

      System.out.println(formDat.parse(formatted));

      try {
          Scanner dateRead = new Scanner(dFileD);
          while (dateRead.hasNextLine()) {
              date = dateRead.nextLine();
              Date dateFroText = formDat.parse(date);
              System.out.println("Date from text new format: " + dateFroText);
          }
          dateRead.close();
        } catch (Exception e) {
            System.out.println("Error!");
        }

      int yearDiff = todAdd.get(Calendar.YEAR) - startDat.get(Calendar.YEAR);
      int dayDiff = ((yearDiff*365) + todAdd.get(Calendar.DAY_OF_YEAR)) - startDat.get(Calendar.DAY_OF_YEAR);
      dayDiff--;
      int hourDiffer = todAdd.get(Calendar.HOUR_OF_DAY)+23 - startDat.get(Calendar.HOUR_OF_DAY);
      int minuDiff = todAdd.get(Calendar.MINUTE)+60 - startDat.get(Calendar.MINUTE);
      System.out.println(dayDiff + " days " + hourDiffer + " hours " + minuDiff +" minutes remaining");


  }

}

1 个答案:

答案 0 :(得分:0)

您需要将日期对象dateFroTxt转换为Calendar对象才能进行计算。

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

        //Get current date and time
        Calendar startDat = Calendar.getInstance();
        System.out.println("Current date: " + startDat.getTime());

        //Get that current date and time and then add 2 days
        Calendar todAdd = Calendar.getInstance();
        todAdd.add(Calendar.DATE, 2);
        System.out.println("Date in 2 days: " + todAdd.getTime());

        //Create a format for sending date to text file
        SimpleDateFormat formDat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
        String formatted = formDat.format(todAdd.getTime());
        System.out.println("Formatted: " + formatted);

        try {
            PrintWriter dW = new PrintWriter("date.txt");
            dW.println(formatted);
            dW.close();
        } catch (IOException e) {
        }

        System.out.println(formDat.parse(formatted));

        Date dateFroText = null;
        try {
            Scanner dateRead = new Scanner(dFileD);
            while (dateRead.hasNextLine()) {
                date = dateRead.nextLine();
                dateFroText = formDat.parse(date);
                System.out.println("Date from text new format: " + dateFroText);
            }
            dateRead.close();
        } catch (Exception e) {
            System.out.println("Error!");
        }

        if (dateFroText != null) {
            Calendar dateFromTxtCal = Calendar.getInstance();
            dateFromTxtCal.setTime(dateFroText);
            int yearDiff = dateFromTxtCal.get(Calendar.YEAR) - startDat.get(Calendar.YEAR);
            int dayDiff = ((yearDiff * 365) + dateFromTxtCal.get(Calendar.DAY_OF_YEAR)) - startDat.get(Calendar.DAY_OF_YEAR);
            dayDiff--;
            int hourDiffer = dateFromTxtCal.get(Calendar.HOUR_OF_DAY) + 23 - startDat.get(Calendar.HOUR_OF_DAY);
            int minuDiff = dateFromTxtCal.get(Calendar.MINUTE) + 60 - startDat.get(Calendar.MINUTE);
            System.out.println(dayDiff + " days " + hourDiffer + " hours " + minuDiff + " minutes remaining");
        }
    }