【问题标题】:how to convert string into date format [closed]如何将字符串转换为日期格式[关闭]
【发布时间】:2012-10-12 05:20:14
【问题描述】:

我得到了date="20120120" 的价值,我想将其转换为日期格式,例如 它应该打印2012-01-20 [YYYY-MM-dd]

【问题讨论】:

标签: java android date


【解决方案1】:

试试这个代码

 String mytime="20120120";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyyMMdd");
        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println("rrrrrrrrrrrrr"+finalDate);

【讨论】:

    【解决方案2】:

    试试这个功能。

    public static String convertStringToDate(String startDate) throws ParseException
    {
        String myFormatString = "yyyyMdd"; // your current date format
        SimpleDateFormat df = new SimpleDateFormat(myFormatString);
        Date startingDate = df.parse(startDate);
    
        DateFormat dateFormat= new SimpleDateFormat("yyyy-M-dd");  // Date format you want
        return dateFormat.format(startingDate);
    }   
    

    【讨论】:

      【解决方案3】:

      以下是转换数据的方法:

      public String DateConverter(String date) {
          SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
          Date dt = null;
          try {
              dt = smf.parse(start_of_event);
              // et = smf.parse(end_of_event);
          } catch (ParseException e1) {
              e1.printStackTrace();
          } catch (java.text.ParseException e) {
              e.printStackTrace();
          }
          smf = new SimpleDateFormat("EEE MMMM dd,yyyy");
          date = smf.format(dt);
          return date;
      }
      

      【讨论】:

        【解决方案4】:

        这对你有用:

        SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
        Date d = dateFormat.parse("string")
        

        您也可以关注link here

        【讨论】:

          【解决方案5】:
          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
          Date date = new Date();
          System.out.println(dateFormat.format(date));
          

          【讨论】:

            猜你喜欢
            • 2023-03-11
            • 2020-05-24
            • 2014-04-13
            • 1970-01-01
            • 2013-12-28
            • 2015-07-13
            • 1970-01-01
            相关资源
            最近更新 更多