java - 将时间转换为英文单词

标签 java arrays time

我正在实现一个程序,该程序根据输入的英文名称返回某个时间点的时间。例如,“两点过十分钟”或“三点半”。程序必须提示用户一次。

System.out.println("time is hours:minutes")

用户不应分别输入小时和分钟。输入必须是小时冒号分钟。对于方法主体,我在数组返回时收到编译错误。

public class TimeLab {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);
  Date d = new Date();
  SimpleDateFormat SDF = new SimpleDateFormat("hh:mm");
  System.out.println("Enter the time");
  String sTime = SDF.format(d).toString();
  sTime = in .nextLine();
  int hours = Integer.parseInt(sTime);
  int minutes = Integer.parseInt(sTime);


  // output the current name of time, hours, minutes.

  // System.out.println("Current hours:minutes is " + sTime);

  System.out.println("Time is: " + getTimeName(hours, minutes));

  // prompt user for hours and minutes, make sure that the user is
  // notified we are using 12 hour time
  // filter against anything <1 and >12 for hours, <0 and >60 for minutes
  //
  // output the name of the imput time.
 }
 public static String getTimeName(int hours, int minutes) {

  if ((hours >= 1 && hours <= 12) && (minutes >= 0 && minutes <= 59)) {

   String hour_mint[] = {
    "",
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten",
    "Eleven",
    "Twelve",
    "Thirteen",
    "Fourteen",
    "Fifteen",
    "Sixteen",
    "Seventeen",
    "Eighteen",
    "Nineteen",
    "Twenty",
    "Twenty one",
    "Twenty two",
    "Twenty three",
    "Twenty four",
    "Twenty five",
    "Twenty six",
    "Twenty seven",
    "Twenty eight",
    "Twenty nine"
   };

   String a;
   if (hours == 12)
    a = hour_mint[1]; // put 'one' if hour is 12
   else
    a = hour_mint[hours + 1]; // if hour is not 12 then store an hour ahead of given hour 

   System.out.print("time is : " + hours + ":" + minutes + ".");

   if (minutes == 0)
    System.out.println(hour_mint[hours] + "o'clock");
   else if (minutes == 15)
    System.out.println("Quarter past " + hour_mint[hours]);
   else if (minutes == 30)
    System.out.println("Half past" + hour_mint[hours]);
   else if (minutes == 45)
    System.out.println("Quarter to" + a);
   else if (minutes < 30) // for minutes between 1-29
    System.out.println(hour_mint[minutes] + " " + "past" + hour_mint[hours]);
   else // between 31-59
    System.out.println(hour_mint[60 - minutes] + " " + "to " + a);
  } else
   System.out.println("invalid time ");
  //String hour_mint = null;
  return hour_mint;
 }
}

最佳答案

好吧,我现在明白了。

public class TimeToEnglish{


  public static void main(String[] args) 
  { 

    Scanner in = new Scanner(System.in); 
    Date d = new Date();
    SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
    System.out.println("Enter the time");
    String sTime = SDF.format(d).toString();
    sTime=in.nextLine();
    String sHours = sTime.substring(0,2);
    String sMinutes = sTime.substring(3);
    try{ int hours = Integer.parseInt(sHours);
      int minutes = Integer.parseInt(sMinutes);
      System.out.println("Time is: "+getTimeName(hours, minutes));
    }

      catch( NumberFormatException e){
        System.out.println("invalid input");//affiche une erreur ici
      }
    }
    public static String getTimeName(int hours, int minutes){
      String time_name = ""; 

      if((hours>=1 && hours<=12) && (minutes>=0 && minutes<=59)){

        String hour_mint []={"", "One", "Two", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine","Ten",
          "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",
          "Twenty","Twenty one", "Twenty two", "Twenty three", "Twenty four", "Twenty five",
          "Twenty six","Twenty seven","Twenty eight", "Twenty nine"};


        String a;
        if (hours==12)
          a = hour_mint [1];// put 'one' if hour is 12
        else 
          a = hour_mint[hours+1]; // if hour is not 12 then store an hour ahead of given hour 

        System.out.print("time is : "+hours+":"+minutes+".");

        if (minutes==0)
          time_name = hour_mint[hours]+"o'clock";
        else if (minutes==15)
          time_name = "Quarter past "+hour_mint[hours];
        else if (minutes==30)
          time_name = "Quarter past "+hour_mint[hours];
        else if (minutes==45)
          time_name = "Quarter to"+a;
        else if (minutes<30) // for minutes between 1-29
          time_name = hour_mint[minutes]+" past "+hour_mint[hours];      
        else // between 31-59
          time_name = hour_mint[60-minutes]+" to "+a;

      }
      else 
        time_name = "invalid time format";

      return time_name;
    }
  }

关于java - 将时间转换为英文单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417473/

相关文章:

java - 用于 Java 的 Finagle Thrift 生成器

java - 检测 if 和 else 语句执行了多长时间

javascript - 本地存储在页面重新加载时不断重置

c++ - 不正确的排序和/或搜索通过外部文件填充的数组

php - PHP 中有从数组中提取 'column' 的函数吗?

php - 通过从 curl_getinfo() 获取详细信息来获取 cURL 请求的时间

arrays - 根据月份确定冬季或夏季时间(UTC 时区)

java - Android编程: How do I get out of a while loop?

java - 图像压缩软件

php - php中的小时计算