代码之家  ›  专栏  ›  技术社区  ›  fermoga

在Java中将字符串日期格式化为其他格式

  •  2
  • fermoga  · 技术社区  · 7 年前

    我正在尝试格式化一个JSON,其中包含 date 输入格式:

    日期:“2017-05-23T06:25:50”

    我当前的尝试如下:

    private String formatDate(String date) {
      SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");
      String formattedDate = "";
    
      try {
        formattedDate = format.format(format.parse(String.valueOf(date)));
      } catch (ParseException e) {
        e.printStackTrace();
      }
    
      return formattedDate;
    }
    

    但在这种情况下,结果不会显示在我的应用程序中 TextView 不可见,我无法记录任何内容。

    我确实尝试过其他解决方案,但没有成功。我不知道是不是因为传入值是字符串。

    6 回复  |  直到 7 年前
        1
  •  9
  •   Ramesh sambu    7 年前

    使用此代码设置“2017-05-23T06:25:50”的格式

    String strDate = "2017-05-23T06:25:50";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(strDate);
        SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
        String finalDateString = sdfnewformat.format(convertedDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    已转换的 finalDateString 设置为您的 textView

        2
  •  2
  •   Harshit kyal    7 年前

    这对你有用。

    String oldstring= "2017-05-23T06:25:50.0";
    Date datee = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(oldstring);
    
        3
  •  2
  •   aslamhossin    7 年前

    这段代码对我有用:

     public static String getNewsDetailsDateTime(String dateTime) {
        @SuppressLint("SimpleDateFormat") DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        Date date = null;
        try {
            date = format.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        @SuppressLint("SimpleDateFormat") String strPublishDateTime = new SimpleDateFormat("MMM d, yyyy h:mm a").format(date);
        return strPublishDateTime;
    }
    

    输出格式为:2017年12月20日下午2:30。

        4
  •  1
  •   Vidhi Dave    7 年前

    使用此功能:

     private String convertDate(String dt) {
    
                //String date="2017-05-23T06:25:50";
    
                try {
                    SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //date format in which your current string is
                    Date newDate = null;
                    newDate = spf.parse(dt);
                    spf = new SimpleDateFormat("MM/DD/yyyy"); //date format in which you want to convert
                    dt = spf.format(newDate);
                    System.out.println(dt);
    
                    Log.e("FRM_DT", dt);
    
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return dt;
    
            }
    
        5
  •  1
  •   Anonymous    7 年前

    TL;博士

    private static final DateTimeFormatter DATE_FORMATTER 
            = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    
    private static String formatDate(String date) {
        return LocalDateTime.parse(date).format(DATE_FORMATTER);
    }
    

    现在 formatDate("2017-05-23T06:25:50") 返回所需的字符串 05/23/2017 .

    Java语言时间

    2017年,我看不出有什么理由让你与早已过时、臭名昭著的麻烦作斗争 SimpleDateFormat java.time ,即现代Java日期和时间API,也称为JSR-310,使用起来非常方便。

    通常,从一种日期时间格式转换为另一种格式时,需要两个格式化程序,一个用于解析输入格式,另一个用于格式化为输出格式。不在这里。这是因为你的字符串 2017-05-23T06:25:50 是ISO 8601格式,这是现代类默认解析的标准,也就是说,没有显式格式化程序。因此,我们只需要一个格式化程序来格式化。

    你的代码出了什么问题

    当我运行你的代码时 ParseException: Unparseable date: "2017-05-23T06:25:50" . 如果您还没有注意到异常,那么您的项目设置中就有一个严重的缺陷,它会向您隐藏有关错误的重要信息。请解决第一件事。

    A. ParseException 有一个方法 getErrorOffset (有点忽略),在本例中返回4。字符串中的偏移量4是第一个连字符所在的位置。所以在格式解析时 MM/DD/yyyy 你的 日期格式 接受2017年为一个月(有趣,不是吗?),然后,应为斜杠,但得到了连字符,因此引发了异常。

    格式模式字符串中还有一个错误:大写 DD 表示一年中的某一天(本例中为143)。小写字母 dd 应用于每月的某一天。

    问题:我可以使用 Java语言时间 在Android上?

    是的,你可以。它至少需要Java 6. .

    • 在Java 8和更高版本中,内置了新的API。
    • 在Java 6和7中,获取三个十个后端口,即新类的后端口(三个十用于JSR 310)。
    • 在Android上 ,使用Android版本的ThreeTen Backport。它被称为ThreeTenABP。

    链接

        6
  •  0
  •   ahuemmer Freddan    2 年前

    尝试以下操作:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(sdf.parse(dt));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
    String output = sdf1.format(c.getTime());