Android: Convert date to milliseconds

后端 未结 7 1247
囚心锁ツ
囚心锁ツ 2021-01-02 13:01

I used

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String time = formatter.format(new Date());

to get t

相关标签:
7条回答
  • 2021-01-02 13:41

    java.time and desugaring or ThreeTenABP

    I recommend you use java.time, the modern Java date and time API, for your date and time work.

        ZoneId zone = ZoneId.systemDefault();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu, H:mm");
        
        String dateTimeString = "12.03.2012, 17:31";
        
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
        long milliseconds = dateTime.atZone(zone).toInstant().toEpochMilli();
        
        System.out.println(milliseconds);
    

    When I run this in Europe/Zurich time zone, the output is:

    1331569860000

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

    java.time works nicely on both older and newer Android devices. It just requires at least 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 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.

    Links

    • Oracle tutorial: Date Time explaining how to use java.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.
    0 讨论(0)
  • 2021-01-02 13:43

    Using this method you can Convert your Date Into miliseconds that can be used to add events to the Calender

    public Long GettingMiliSeconds(String Date)
        {
            long timeInMilliseconds = 0;
    
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
            try {
    
                Date mDate = sdf.parse(Date);
                timeInMilliseconds = mDate.getTime();
    
            } catch (ParseException  e) {
                e.printStackTrace();
            }
    
            return  timeInMilliseconds;
        }
    
    0 讨论(0)
  • 2021-01-02 13:49

    If you want the current time in millis just use System.currentTimeMillis()

    0 讨论(0)
  • 2021-01-02 13:49

    Use .getMillis();

    e.g:

    DateTime dtDate = new DateTime();
    dtDate.getMillis()
    
    0 讨论(0)
  • 2021-01-02 13:50

    The simplest way is to convert Date type to milliseconds:

    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
    formatter.setLenient(false);
    
    Date curDate = new Date();
    long curMillis = curDate.getTime();
    String curTime = formatter.format(curDate);
    
    String oldTime = "05.01.2011, 12:45";
    Date oldDate = formatter.parse(oldTime);
    long oldMillis = oldDate.getTime();
    
    0 讨论(0)
  • 2021-01-02 13:54
    String Date = "Tue Apr 25 18:06:45 GMT+05:30 2017"; 
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    try {
        Date mDate = sdf.parse(Date);
        long timeInMilliseconds = mDate.getTime();
    
    } catch (ParseException e) {
                e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题