java字符串截取去掉月份或天多余的0


    //去掉月份或天多余的0 ======2018-01-03 ===>  2018-1-3

 

   private String simplify(String date){
      int index1 =date.indexOf("-");
      int index2 =date.lastIndexOf("-");
      int month;
      int day;
      int year=Integer.parseInt(date.substring(0,index1));
      if(date.substring(index1+1,index1+2).equals("0")){
          month=Integer.parseInt(date.substring(index1+2,index1+3));
      }else {
          month=Integer.parseInt(date.substring(index1+1,index1+3));
      }
      if(date.substring(index2+1,index2+2).equals("0")){
          day=Integer.parseInt(date.substring(index2+2,index2+3));
      }else{
          day=Integer.parseInt(date.substring(index2+1,index2+3));
      }
      String date1=(year+"-"+month+"-"+day)+"";
      return  date1;
  }


 

你可能感兴趣的:(java,android,date)