Advertisement
Guest User

quy doi 24h

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. public String test(String fulltime){
  2.     // Kiem tra xem string time co ky tu : phan cach hay khong
  3.     if(fulltime.indexOf(":")==-1)
  4.     {
  5.         throw new IllegalArgumentException("Khong hop le");
  6.     }
  7.    
  8.     // Chia ra 2 strings - Trong do time[0] la gio, time[1] la phut
  9.     String[] time = fulltime.split(":");
  10.    
  11.     // Kiem tra length cua 2 chuoi xem co hop le hay khong
  12.     if(time[0].length() > 2 || time[0].length() < 1)
  13.     {
  14.         throw new IllegalArgumentException("Gio khong hop le");
  15.     }
  16.     if(time[1].length() > 2 || time[1].length() < 1)
  17.     {
  18.         throw new IllegalArgumentException("Phut khong hop le");
  19.     }
  20.      
  21.     // Kiem tra gia tri gio co hop le hay khong (0-24)
  22.     if(time[0].length()==1)
  23.     {
  24.         if(time[0].charAt(0) < '0' || time[0].charAt(0) > '9')
  25.         {
  26.           throw new IllegalArgumentException("Gio khong hop le");
  27.         }
  28.     }else{
  29.         // Neu length la 2 ky tu thi kiem tra xem co hop le hay khong
  30.         // Kiem tra ky tu dau tien (phai la 0->2 vi toi da la 24h)
  31.         if(time[0].charAt(0) < '0' || time[0].charAt(0) > '2' )
  32.         {
  33.           throw new IllegalArgumentException("Gio khong hop le");
  34.         }
  35.         // Kiem tra ky tu thu 2 (0->9)
  36.         if(time[0].charAt(1) < '0' || time[0].charAt(1) > '9')
  37.         {
  38.           throw new IllegalArgumentException("Gio khong hop le");
  39.         }
  40.     }
  41.     if(Integer.parseInt(time[0]) > 23){
  42.         throw new IllegalArgumentException("Gio khong hop le");
  43.     }
  44.    
  45.     // Kiem tra gia tri phut co hop le hay khong
  46.     if(time[1].length()==1)
  47.     {
  48.         if(time[1].charAt(0) < '0' || time[1].charAt(0) > '9')
  49.         {
  50.           throw new IllegalArgumentException("Gio khong hop le");
  51.         }
  52.         // Them so 0 vao dau neu chi dien 1 ky tu
  53.         time[1] = "0"+time[1];
  54.     }else{
  55.         // Neu length la 2 ky tu thi kiem tra xem co hop le hay khong
  56.         // Kiem tra ky tu dau tien (phai la 0->6 vi toi da la 60 phut)
  57.         if(time[1].charAt(0) < '0' || time[1].charAt(0) > '6' )
  58.         {
  59.           throw new IllegalArgumentException("Phut khong hop le");
  60.         }
  61.         // Kiem tra ky tu thu 2 (0->9)
  62.         if(time[1].charAt(1) < '0' || time[1].charAt(1) > '9')
  63.         {
  64.           throw new IllegalArgumentException("Phut khong hop le");
  65.         }
  66.     }
  67.     if(Integer.parseInt(time[1]) > 59){
  68.         throw new IllegalArgumentException("Phut khong hop le");
  69.     }
  70.    
  71.     // Quy doi gio
  72.     if(Integer.parseInt(time[0]) == 0 )
  73.     {
  74.         return "12:"+time[1]+"am";
  75.     }else if(Integer.parseInt(time[0]) > 11 ){
  76.         int hour = Integer.parseInt(time[0])-12;
  77.         if(hour > 0)
  78.         {
  79.             return hour+":"+time[1]+"pm";
  80.         }else{
  81.             return time[0]+":"+time[1]+"pm";
  82.         }
  83.     }else{
  84.         return time[0]+":"+time[1]+"am";
  85.     }
  86.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement