Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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
java中的日期格式_Java_Android - Fatal编程技术网

java中的日期格式

java中的日期格式,java,android,Java,Android,我得到的回应是 "pickTime": "Tue Oct 25 03:57 PM" 我已按如下方式编码以获取 SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a"); SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd"); SimpleDateFormat time

我得到的回应是

 "pickTime": "Tue Oct 25 03:57 PM" 
我已按如下方式编码以获取

SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");
                SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
                SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");

String time = jsonObj.optString("pickTime");
Date dateObj = curFormater.parse(time); 
String newDateStr = dateFormater.format(dateObj); 
String newTimeStr = timeFormater.format(dateObj);
问题是我在字符串
time
中得到的值与在dateobj中得到的值相同

"sun oct 25 15.57 pm"
newDatestr as

"sun oct 25"

请帮我解决这个问题,提前谢谢

这是1970年10月25日(Linux时间)

您必须弄清楚如何将年份添加到日期,因为它不是当前年份

它采用默认值(linux时间从1970年1月1日开始)

这就是为什么你要把E

SimpleDateFormat curFormater = new SimpleDateFormat("EEE MM dd yyyy hh:mm a");

我认为这应该是一种格式。

您的回复没有包含年份。所以您得到的输出不正确

下面的代码正在工作,请尝试一下

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a yyyy");
        SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
        SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");

        String time = "Tue Oct 25 03:57 PM" + " " + calendar.get(Calendar.YEAR);
        Date dateObj = null;
        try {
            dateObj = curFormater.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Logger.logd(this, dateObj.toString());

        String newDateStr = dateFormater.format(dateObj);
        String newTimeStr = timeFormater.format(dateObj);
        Logger.logd(this, newDateStr + " " + newTimeStr);

@大卫·马西尔是对的。您应该指定年份。

您将从您的
pickTime
值中受益一年。此外,请检查您的设备的日期:最后一个星期日是2015年10月25日。我不会收到年份响应,那么我该怎么做呢?。正如您所说,我理解没有年份,默认值为1970年,对吗?
pickTime+==“currentYear
,将其添加到
SimpleDateFormat
模式中。简要解释一下,我需要做什么do@Daryl:您必须定义如何确定年份。这是关于业务需求的。这没有任何意义,如果年份不在我知道的日期范围内,我会在不使用年份的情况下寻求解决方案。请参阅更新的评论。我已经这样做了。但不会总是今年,因为如果我们是在2016年12月,选择时间可能是2017年(响应)。那时它不会工作。是的。那么它应该有年份,否则我们就得不到它。你确定吗,如果没有得到年份的响应,就不可能得到预期的解决方案。如何纠正它,年份不会有响应。那么有没有可能解决我的问题呢。如果是这样,请告诉我。是的,使用“int year=Calendar.getInstance().get(Calendar.year);”来获取年份谢谢你的帮助。我已经这样做了。但不会总是今年,因为如果我们在2016年12月,拾取时间可能在2017年(响应)。那时它不起作用。然后你必须比较日期,如果大于实际值,则少一年。如果(Calendar.getInstance().before(dateCreated)){dateCreated.add(Calendar.MONTH,-1);}
Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a yyyy");
        SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
        SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");

        String time = "Tue Oct 25 03:57 PM" + " " + calendar.get(Calendar.YEAR);
        Date dateObj = null;
        try {
            dateObj = curFormater.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Logger.logd(this, dateObj.toString());

        String newDateStr = dateFormater.format(dateObj);
        String newTimeStr = timeFormater.format(dateObj);
        Logger.logd(this, newDateStr + " " + newTimeStr);
  SimpleDateFormat formatter =new SimpleDateFormat("EEE MMM dd hh:mm a");
     Date date2 = formatter.parse("Tue Oct 25 03:57 PM");
    long time = date2.getTime();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);
    cal.set(Calendar.YEAR, 2016);
    String formatted = formatter.format(cal.getTime());

    System.out.println(formatted);