Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅解析DateFormat Java中的年份_Java_Android - Fatal编程技术网

仅解析DateFormat Java中的年份

仅解析DateFormat Java中的年份,java,android,Java,Android,我得到一个返回的解析JSON结果,其字符串值的形式为日期,如“27-11-2012”,我将其解析为日期对象。我的代码是: public Date stringToDateReport(String s){ //Log.d(TAG, "StringToDateReport here is " + s); DateFormat format; Date date = null; //if(s.matches(""))

我得到一个返回的解析JSON结果,其字符串值的形式为日期,如“27-11-2012”,我将其解析为日期对象。我的代码是:

public Date stringToDateReport(String s){
        //Log.d(TAG,    "StringToDateReport here is " + s);
        DateFormat format;
        Date date = null;

        //if(s.matches(""))
         format = new SimpleDateFormat("dd-MMM-yyyy");

        try {

            date = (Date)format.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
现在我的问题是,已经实现了一个特性,有时候json只返回一个year对象,比如“2012”,并像预期的那样给我一个“ParseException:Unparseable date”。我曾考虑使用正则表达式来匹配字符串模式并从中解析,但不确定如何做到这一点。有什么想法吗?还有,用DateFormat只解析一年吗?

我会试试:

public Date stringToDateReport(String s){
    DateFormat format;
    Date date = null;

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

    if(s.length()==4) {
        format = new SimpleDateFormat("yyyy");
    }
    try {
        date = (Date)format.parse(s);
    } catch (ParseException e) {
        //you should do a real logging here
        e.printStackTrace();
    }
    return date;
}
背后的逻辑是检查字符串是否只有4长,然后应用不同的格式。在这种情况下,这个简单的方法就足够了,但在其他方法中,可能需要使用正则表达式。

我会尝试:

public Date stringToDateReport(String s){
    DateFormat format;
    Date date = null;

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

    if(s.length()==4) {
        format = new SimpleDateFormat("yyyy");
    }
    try {
        date = (Date)format.parse(s);
    } catch (ParseException e) {
        //you should do a real logging here
        e.printStackTrace();
    }
    return date;
}
背后的逻辑是检查字符串是否只有4长,然后应用不同的格式。在这种情况下,这个简单的方法就足够了,但在其他方法中,可能需要使用正则表达式。

试试这段代码

public Date stringToDateReport(String s){
    //Log.d(TAG,    "StringToDateReport here is " + s);
    DateFormat format;
    Date date = null;

    if(s.indexOf("-") < 0){
     format = new SimpleDateFormat("yyyy");
    }else{
     format = new SimpleDateFormat("dd-MMM-yyyy");
    }
    try {

        date = (Date)format.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
公共日期字符串ToDaterReport(字符串s){
//Log.d(标记“StringToDaterReport此处为”+s);
日期格式;
日期=空;
如果(s.indexOf(“-”)小于0){
格式=新的SimpleDataFormat(“yyyy”);
}否则{
格式=新的SimpleDataFormat(“dd MMM yyyy”);
}
试一试{
date=(date)format.parse;
}捕获(解析异常){
e、 printStackTrace();
}
返回日期;
}
是否有可能在
字符串中使用另一种格式?还是这两个?

试试这段代码

public Date stringToDateReport(String s){
    //Log.d(TAG,    "StringToDateReport here is " + s);
    DateFormat format;
    Date date = null;

    if(s.indexOf("-") < 0){
     format = new SimpleDateFormat("yyyy");
    }else{
     format = new SimpleDateFormat("dd-MMM-yyyy");
    }
    try {

        date = (Date)format.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
public Date stringToDateReport(String strDate){
    DateFormat formatnew SimpleDateFormat("dd-MM-yyyy");
    Date date = null;

    if(strDate.length()==4) {
        format = new SimpleDateFormat("yyyy");
    }
    try {
        date = (Date)format.parse(strDate);
    } catch (ParseException e) {
        //error parsing date
        e.printStackTrace();
    }
    return date;
}
公共日期字符串ToDaterReport(字符串s){
//Log.d(标记“StringToDaterReport此处为”+s);
日期格式;
日期=空;
如果(s.indexOf(“-”)小于0){
格式=新的SimpleDataFormat(“yyyy”);
}否则{
格式=新的SimpleDataFormat(“dd MMM yyyy”);
}
试一试{
date=(date)format.parse;
}捕获(解析异常){
e、 printStackTrace();
}
返回日期;
}
是否有可能在
字符串中使用另一种格式?还是就这两个

public Date stringToDateReport(String strDate){
    DateFormat formatnew SimpleDateFormat("dd-MM-yyyy");
    Date date = null;

    if(strDate.length()==4) {
        format = new SimpleDateFormat("yyyy");
    }
    try {
        date = (Date)format.parse(strDate);
    } catch (ParseException e) {
        //error parsing date
        e.printStackTrace();
    }
    return date;
}
那么就这样称呼它:

String strDate = yourJson.getString("date");
Date d = stringToDateReport(strDate);
那么就这样称呼它:

String strDate = yourJson.getString("date");
Date d = stringToDateReport(strDate);

format=新的SimpleDataFormat(“yyyy”)
@Houcine问题是,有时发送完整字符串,有时仅发送年份…
format=new SimpleDateFormat(“yyy”)
@Houcine问题是,有时会发送完整的字符串,其他时间,仅在一年内发送……您的意思是
s.indexOf(“-”)<0
?如果没有“-”的话应该是-1。斯派洛:别担心!我们是来帮忙的!:)你的意思是
s.indexOf(“-”)<0
?如果没有“-”的话应该是-1。斯派洛:别担心!我们是来帮忙的!:)谢谢我真的应该想到这一点。再次感谢!谢谢我真的应该想到这一点。再次感谢!