java SimpleDateFormat 给出 API 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39055963/
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-11-03 04:03:00  来源:igfitidea点击:

SimpleDateFormat gives API Error

javaandroid

提问by Azrie Bakri

Whenever I use my SimpleDateFormatin android it keep giving me the error call requires API level 24. Which doesn't make sense since I refer to thistutorial which was a couple of years old.

每当我SimpleDateFormat在 android 中使用它时,它一直给我错误调用需要 API 级别 24。这没有意义,因为我参考了几年前的教程。

This is the code that giving out the error

这是给出错误的代码

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss");

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss");

I even try but it's still not working

我什至尝试,但它仍然无法正常工作

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss",Locale.US);

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss",Locale.US);

Here is my method where I'm having problem with

这是我遇到问题的方法

public static long getDateInMillis(String srcDate) {

    SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM yyyy HH:mm:ss");

    long dateInMillis = 0;
    try {
        Date date = formatter.parse(srcDate);
        dateInMillis = date.getTime();
        return dateInMillis;
    }

    catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    return 0;
}

回答by EpicPandaForce

回答by ênio Rodrigues

Change your import

更改您的导入

import android.icu.text.SimpleDateFormat;

to

import java.text.SimpleDateFormat;