如何将军事时间改为小数时间?

时间:2015-07-30 22:05:03

标签: java date datetime datetime-conversion

我在getDecimalTime方法中遇到了一些问题。我不知道是否有某种公式可以帮助我将军事时间(例如0645 - > 06:04 am)改为小数时间。

public class TimeInterval
{
   int time1;
   int time2;
   int hours;
   int minutes;
   double decTime;
   public TimeInterval()
   {
      time1 = 0;
      time2 = 0;
   }
   public TimeInterval(int _time1, int _time2)
   {
      time1 = _time1;
      time2 = _time2;
   }

   public int getHours()
   {

      if(time1 > time2)
      {
         hours = (time1 - time2) / 100;
      }
      else if(time1 < time2)
      {
         hours = (time2 - time1) / 100;
      } 
      return hours;
   }

   public int getMinutes()
   {
      if(time1 > time2)
      {
         minutes = (time1 - time2) % 100;
      }
      else if(time1 < time2)
      {
         minutes = (time2 - time1) % 100;
      } 
      return minutes;
   }
   public double getDecimalTime()
   {
      return decTime;
   }
}

测试器:

import java.util.*;

public class TestTimeInterval
{
   public static void main(String[] args)
   {

      Scanner in = new Scanner(System.in);

      System.out.println("Please enter the first time: ");
      int _time1 = in.nextInt();

      System.out.println("Please enter the second time: ");
      int _time2 = in.nextInt();

      TimeInterval object = new TimeInterval(_time1,_time2);

      System.out.println("Elapsed time in hrs/min: " + object.getHours() + " hours " + object.getMinutes() + " minutes");
      System.out.println("Elapsed time in decimal: " + object.getDecimalTime() + " hours");


   }
}

0 个答案:

没有答案