为什么SimpleDateFormat在这种情况下将月份作为1月而不是10月? [英] Why SimpleDateFormat is giving Month as January instead of October in this case?

查看:278
本文介绍了为什么SimpleDateFormat在这种情况下将月份作为1月而不是10月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种转换日期的方法

I have this method for converting a date

我希望将日期作为 10月30日返回,但为什么会返回 1月10日

I am expecting date to be returned as Oct-30 , but why is it returning Jan-10

这是我的程序

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {
    public static void main(String[] args) throws ParseException {
         String today = convertdate("10/30/2015");
            System.out.println("Today : " + today);
    }
    public static String convertdate(String recivieddate) throws ParseException {
        SimpleDateFormat in = new SimpleDateFormat("dd/mm/yyyy");
        Date date = in.parse(recivieddate);
        SimpleDateFormat out = new SimpleDateFormat("MMM-dd");
        String newdate = out.format(date);
        return newdate;
    }
}

您能否告诉我如何解决此问题?

Could you please let me know how to resolve this issue?

推荐答案

你的模式令人困惑。你在几天之前使用几个月, mm 是分钟,而不是月份,这是 MM

You are confusing patterns. You use months before days, and mm is for minute, not month, which is MM

=> SimpleDateFormat in = new SimpleDateFormat(MM / dd / yyyy);

=> SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy");

public class Test {
    public static void main(String[] args) throws ParseException {
         String today = convertdate("10/30/2015");
            System.out.println("Today : " + today);
    }
    public static String convertdate(String recivieddate) throws ParseException {
        SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy");
        Date date = in.parse(recivieddate);
        SimpleDateFormat out = new SimpleDateFormat("MMM-dd");
        String newdate = out.format(date);
        return newdate;
    }
}

这篇关于为什么SimpleDateFormat在这种情况下将月份作为1月而不是10月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆