简单的东西不解释直接上代码:

  StringBuffer s=new StringBuffer("20190305").insert(4,"-").insert(7,"-");
  System.out.println(s);

=============================================================================

   public   String strToDateFormat(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(newDate);
    }
        try {
            System.out.println(this.strToDateFormat("20190305"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐