简体   繁体   English

如何将字符串Date转换为long millseconds

[英]How to convert a string Date to long millseconds

I have a date inside a string, something like "12-December-2012". 我在字符串中有一个日期,类似于“2012年12月12日”。 How can I convert this into milliseconds (long)? 如何将其转换为毫秒(长)?

Using SimpleDateFormat 使用SimpleDateFormat

String string_date = "12-December-2012";

SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
try {
    Date d = f.parse(string_date);
    long milliseconds = d.getTime();
} catch (ParseException e) {
    e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = (Date)formatter.parse("12-December-2012");
long mills = date.getTime();

看一下SimpleDateFormat类,它可以解析String并返回DateDate类的getTime方法。

It's about time someone provides the modern answer to this question. 现在是时候有人为这个问题提供了现代答案。 In 2012 when the question was asked, the answers also posted back then were good answers. 在2012年问到这个问题的时候,那些回答的答案也是很好的答案。 Why the answers posted in 2016 also use the then long outdated classes SimpleDateFormat and Date is a bit more of a mystery to me. 为什么2016年发布的答案也使用当时很久的过时类SimpleDateFormatDate对我来说有点神秘。 java.time , the modern Java date and time API also known as JSR-310, is so much nicer to work with. java.time ,现代Java日期和时间API,也称为JSR-310,使用起来非常好。 You can use it on Android through the ThreeTenABP, see this question: How to use ThreeTenABP in Android Project . 您可以通过ThreeTenABP在Android上使用它,请参阅此问题:如何在Android项目中使用ThreeTenABP

For most purposes I recommend using the milliseconds since the epoch at the start of the day in UTC . 对于大多数用途,我建议使用自UTC时间开始的纪元以来的毫秒数。 To obtain these: 要获得这些:

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("d-MMMM-uuuu", Locale.ENGLISH);
    String stringDate = "12-December-2012";
    long millisecondsSinceEpoch = LocalDate.parse(stringDate, dateFormatter)
            .atStartOfDay(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();
    System.out.println(millisecondsSinceEpoch);

This prints: 这打印:

1355270400000

If you require the time at start of day in some specific time zone, specify that time zone instead of UTC, for example: 如果您需要在某个特定时区的某天开始时间,请指定该时区而不是UTC,例如:

            .atStartOfDay(ZoneId.of("Asia/Karachi"))

As expected this gives a slightly different result: 正如预期的那样,结果略有不同:

1355252400000

Another point to note, remember to supply a locale to your DateTimeFormatter . 还有一点需要注意,请记住为DateTimeFormatter提供一个语言环境。 I took December to be English, there are other languages where that month is called the same, so please choose the proper locale yourself. 我把12月份当作英语,还有其他语言,那个月被称为相同,所以请自己选择合适的语言环境。 If you didn't provide a locale, the formatter would use the JVM's locale setting, which may work in many cases, and then unexpectedly fail one day when you run your app on a device with a different locale setting. 如果您没有提供语言环境,格式化程序将使用JVM的语言环境设置(在许多情况下可能有效),然后在具有不同语言环境设置的设备上运行应用程序时出现意外故障。

您可以使用simpleDateFormat来解析字符串日期。

using simpledateformat you can easily achieve it. 使用simpledateformat您可以轻松实现它。

1) First convert string to java.Date using simpledateformatter. 1)首先使用simpledateformatter将字符串转换为java.Date。

2) Use getTime method to obtain count of millisecs from date 2)使用getTime方法从日期获得毫秒数

 public class test {
      public static void main(String[] args) {
      String currentDate = "01-March-2016";
      SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
     Date parseDate = f.parse(currentDate);
     long milliseconds = parseDate.getTime();
  }
        }

more Example click here 更多示例请单击此处

Easiest way is used the Date Using Date() and getTime() 最简单的方法是使用Date Using Date()和getTime()

    Date dte=new Date();
    long milliSeconds = dte.getTime();
    String strLong = Long.toString(milliSeconds);
    System.out.println(milliSeconds)

Try below code 试试下面的代码

        SimpleDateFormat f = new SimpleDateFormat("your_string_format", Locale.getDefault());
        Date d = null;
        try {
            d = f.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeInMillis = d.getTime();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM