Android 从长格式转换为日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11773369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:48:55  来源:igfitidea点击:

Convert from Long to date format

androiddatelong-integer

提问by HaOx

I want to convert Long value to String or Date in this format dd/mm/YYYY.

我想以这种格式 dd/mm/YYYY 将 Long 值转换为 String 或 Date。

I have this value in Long format: 1343805819061.

我有这个长格式的值:1343805819061。

It is possible to convert it to Date format?

可以将其转换为日期格式吗?

回答by AAnkit

You can use below line of code to do this. Here timeInMilliSecond is long value.

您可以使用以下代码行来执行此操作。这里的 timeInMilliSecond 是长值。

 String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(TimeinMilliSeccond));

Or you can use below code too also.

或者你也可以使用下面的代码。

 String longV = "1343805819061";
 long millisecond = Long.parseLong(longV);
 // or you already have long value of date, use this instead of milliseconds variable.
 String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();

Reference:- DateFormatand SimpleDateFormat

参考:- DateFormatSimpleDateFormat

P.S. Change date format according to your need.

PS 根据您的需要更改日期格式。

回答by Mark Bakker

You can use method setTime on the Date instance or the contructor Date(long);

您可以在 Date 实例或构造函数 Date(long) 上使用 setTime 方法;

setTime(long time) 
      Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Date(long date) 
      Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT

Then use the simple date formater

然后使用简单的日期格式化程序

see http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

回答by Adeel Pervaiz

java.util.Date dateObj = new java.util.Date(timeStamp);

Here timeStampis your long integer which is actually timestamp in millieseconds, you get the java date object, now you can convert it into string by this

这里timeStamp是你的长整数,它实际上是以毫秒为单位的时间戳,你得到了 java 日期对象,现在你可以通过这个将它转换成字符串

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy");

StringBuilder nowYYYYMMDD = new StringBuilder( dateformatYYYYMMDD.format( dateObj ) );
StringBuilder nowMMDDYYYY = new StringBuilder( dateformatMMDDYYYY.format( dateObj ) );

回答by Ole V.V.

java.time and ThreeTenABP

java.time 和 ThreeTenABP

I am providing the modern answer. I suggest using java.time, the modern Java date and time API, for your date work. This will work on your Android version:

我正在提供现代答案。我建议使用 java.time,现代 Java 日期和时间 API,用于您的日期工作。这将适用于您的 Android 版本:

    // Take Catalan locale as an example for the demonstration
    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
                    .withLocale(Locale.forLanguageTag("ca"));

    long millisecondsSinceEpoch = 1_343_805_819_061L;
    ZonedDateTime dateTime = Instant.ofEpochMilli(millisecondsSinceEpoch)
            .atZone(ZoneId.systemDefault());
    String dateString = dateTime.format(dateFormatter);
    System.out.println("As formatted date: " + dateString);

Output is:

输出是:

As formatted date: 01/08/2012

格式化日期:01/08/2012

I recommend that you use a built-in localized date format for presentation to your user. I took Catalan date format just as an example. Formats for many languages, countries and dialects are built-in.

我建议您使用内置的本地化日期格式向用户展示。我以加泰罗尼亚语日期格式为例。许多语言、国家和方言的格式都是内置的。

The SimpleDateFormatclass used in most of the old answers is a notorious troublemaker of a class. The Dateclass also used is poorly designed too. Fortunately they are both long outdated. It's no longer recommended to use any of those. And I just find java.time so much nicer to work with.

SimpleDateFormat在最老的答案使用类是一类的臭名昭著的麻烦制造者。Date还使用的类也设计得很差。幸运的是,它们都已经过时了。不再建议使用其中任何一个。我只是发现 java.time 更好用。

Question: Doesn't java.time require Android API level 26?

问题:java.time 不需要 Android API 级别 26 吗?

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

java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少Java 6

  • 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 use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bpwith subpackages.
  • 在 Java 8 及更高版本和更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
  • 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

Links

链接

回答by Bilal Haider

if thre is 10 digits in long then try this

如果 thre 是 10 位数字,那么试试这个

long DateInLong= 1584212400;

长 DateInLong = 1584212400;

Date date = new Date(DateInLong*1000L);

日期日期 = 新日期(DateInLong*1000L);

SimpleDateFormat simpledateformate= new SimpleDateFormat("yyyy-MM-dd");

SimpleDateFormat simpledateformate= new SimpleDateFormat("yyyy-MM-dd");

String DATE = simpledateformate.format(date);

String DATE = simpledateformat.format(date);