将字符串转换为andorid中的日期格式 [英] Convert String to Date format in andorid

查看:88
本文介绍了将字符串转换为andorid中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取类似1604341549的字符串,并希望将它们转换为正常的日期格式,例如2012年2月12日4:00.这是我的实现

I am getting the strings like 1604341549 and want to convert them to normal date format like 12 feb 2012 4:00. Here is my implementation

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date d = sdf.parse(date);
            sdf.applyPattern("dd MMM yyyy hh:mm");
            holder.v.setText(sdf.format(d));
        } catch (ParseException ex) {
            Log.e("Exception", ex.getLocalizedMessage());
        }

我已经检查了日志,并在try块之前将日期显示为String,但未在try块中实现,而是给出了错误无法解析的日期:"1604341549"

I have checked the logs and it is showing the dates as String before the try block but it is not implementing in the try block instead it is giving an error " Unparseable date: "1604341549" ".

推荐答案

java.time通过重复修改

考虑使用java.time(现代的Java日期和时间API)进行日期和时间工作.首先,我们声明两个格式化程序,一个用于您的输入,一个用于所需的输出.

java.time through desugaring

Consider using java.time, the modern Java date and time API, for your date and time work. Let’s first declare two formatters, one for your input and one for your desired output.

/** For parsing seconds since the epoch */
private static final DateTimeFormatter unixTimestampFormatter
        = new DateTimeFormatterBuilder()
                .appendValue(ChronoField.INSTANT_SECONDS)
                .toFormatter();

/** For formatting into normal date and time */
private static final DateTimeFormatter normalFormatter
        = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
                .withLocale(Locale.UK);

现在转换是这样的:

    String inputStr = "1604341549";
    Instant inst = unixTimestampFormatter.parse(inputStr, Instant.FROM);
    String normalDateTimeStr = inst.atZone(ZoneId.systemDefault())
            .format(normalFormatter);
    System.out.println(normalDateTimeStr);

输出为:

2020年11月2日19:25:49

02-Nov-2020 19:25:49

我相信Shamm在另一个答案中是正确的:您的输入字符串包含所谓的 Unix时间戳,即自 Unix时期以来的秒数1970年1月1日世界标准时间00:00.借助java.time,我们可以构建一个解析此类的格式化程序.

I am convinced that Shamm is correct in the other answer: your input strings contain so-called Unix timestamps, that is, counts of seconds since the Unix epoch on Jan 1, 1970 at 00:00 UTC. With java.time we can build a formatter that parses such.

为了提供正常"输出,我使用Java的内置日期和时间格式.在这种情况下,使用的是英国语言环境,但是对于大多数内容用户,您应该使用用户的语言环境.

For giving "normal" output I am using Java’s built-in date and time format. In this case for UK locale, but you should use your users’ locale for most content users.

首先,您试图将输入字符串解析为包含年,月,日,时等的字符串,但没有.因此,您很幸运能收到一个异常,因此您知道它是错误的( SimpleDateFormat 通常不会给出该错误,使您相信一切正常都可以).

First, you were trying to parse the input string as containing year, month, date, hour, etc., which it didn’t. So you were lucky to get an exception so you got aware that it’s wrong (SimpleDateFormat very often does not give that, leaving you believing that everything is fine when it isn’t).

SimpleDateFormat 将1604解析为年,将34解析为月,将15解析为月中的一天,将49解析为一天中的小时.然后它提出反对,因为分钟(和秒)还没有数字.

SimpleDateFormat parsed 1604 as year, 34 as month, 15 as day of month and 49 as hour of day. It then objected because there weren’t any digits left for the minutes (and seconds).

java.time在旧的和更新的Android设备上均可正常运行.它只需要至少 Java 6 .

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
  • 在非Android Java 6和7中,获得ThreeTen反向端口,即现代类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
  • 在较旧的Android上,请使用废除旧书或Android版本的ThreeTen Backport.称为ThreeTenABP.在后一种情况下,请确保使用子包从 org.threeten.bp 导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Unix time
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • Java 8+ APIs available through desugaring
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于将字符串转换为andorid中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆