本文介绍了从年,月,日创建java日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int day = Integer.parseInt(request.getParameter(day)); // 25 
int month = Integer.parseInt(request.getParameter(month)); // 12
int year = Integer.parseInt(request.getParameter(year)); // 1988

System.out.println(year);

日历c = Calendar.getInstance();
c.set(year,month,day,0,0);

b.setDob(c.getTime());

System.out.println(b.getDob());

输出为 -

  1988 
Wed Jan 25 00:00:08 IST 1989



25 12 1988 但我得到 25 jan 1989 为什么? >

问题在哪里?



日历.set 这是我遵循的 -

解决方案

所以12被解释为12月+ 1个月。使用

  c.set(year,month  -  1,day,0,0); 


int day = Integer.parseInt(request.getParameter("day"));  //25
int month = Integer.parseInt(request.getParameter("month")); //12
int year = Integer.parseInt(request.getParameter("year")); // 1988

System.out.println(year);

Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0);

b.setDob(c.getTime());

System.out.println(b.getDob());

Output is -

1988
Wed Jan 25 00:00:08 IST 1989

i am passing 25 12 1988 but i get 25 jan 1989 why?

where is the problem ?

here is the Docs for Calendar.set which i followed -

解决方案

Months are zero-based in Calendar. So 12 is interpreted as december + 1 month. Use

c.set(year, month - 1, day, 0, 0);

这篇关于从年,月,日创建java日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:49