【问题标题】:Date format in javajava中的日期格式
【发布时间】:2016-10-25 10:05:55
【问题描述】:

得到响应

 "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);

问题是我在 String time 中得到相同的值,但在 dateobj 中我得到的值是

"sun oct 25 15.57 pm"

newDatestr 为

"sun oct 25"

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

【问题讨论】:

  • 您的pickTime 值将受益于一年。另外,请检查您设备的日期:10 月 25 日的最后一个星期日是 2015 年。
  • 我不会收到一年的回复,那我该怎么办呢?正如你所说,我知道没有年份,默认为 1970,我正确吗?
  • pickTime += " " + currentYear,将其添加到SimpleDateFormat 模式中。
  • 简单解释一下,我要做什么
  • @Daryl:您必须定义如何确定年份。这是关于业务需求的。

标签: java android


【解决方案1】:

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

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

取而代之的是默认值(linux时间从1970年1月1日开始)。

【讨论】:

  • 如何整改,一年不来。那么是否可以解决我的问题。如果是的话告诉我。
  • 是的,使用:“int year = Calendar.getInstance().get(Calendar.YEAR);”获取年份stackoverflow.com/questions/136419/…
  • 感谢您的帮助。我已经这样做了。但它不会总是今年,因为如果我们在 2016 年 12 月,pickTime 可能在 2017 年(响应)。那时它不会工作.
  • 那么你必须比较日期,如果它比实际日期大,那就少一年。 if( Calendar.getInstance().before(dateCreated) ) { dateCreated.add(Calendar.MONTH, -1); }
【解决方案2】:

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

下面的代码可以试一试。

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);

【讨论】:

  • 我知道,我正在寻求解决方案,但不使用年份作为回应。
  • 请查看更新后的评论。我测试了它的工作原理
  • 感谢您的帮助。我已经这样做了。但它不会总是今年,因为如果我们在 2016 年 12 月,pickTime 可能在 2017 年(响应)。那时它不会工作.
  • 是的。那么它应该有年份,否则我们无法得到它。
  • 你确定吗,如果没有一年的响应,就不可能得到预期的解决方案。
【解决方案3】:
  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);

@David Marciel 是对的。你应该指定年份。

【讨论】:

    【解决方案4】:
    SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");
    

    这就是为什么你放了E。

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

    我认为这应该是格式。

    【讨论】:

    • 没有意义,如果年份不在日期字符串中
    猜你喜欢
    • 1970-01-01
    • 2010-11-03
    • 2010-09-29
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多