为什么SimpleDateFormat在这种情况下将月份作为1月而不是10月?
我有这种转换日期的方法
我期待将日期作为10月30日返回,但为什么它会在1月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; } }
你能告诉我如何解决这个问题吗?
你是混乱的模式。 你在几天之前使用几个月, mm是分钟,而不是月份,也就是MM
=> 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; } }
你的格式错了。 它不应该是"dd/mm/yyyy"
,而是"MM/dd/yyyy"
:
SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy");