从年、月、日创建java日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16499228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating java date object from year,month,day
提问by JAVAGeek
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
1988 年
1 月 25日星期三 00:00:08 IST 1989
I am passing 25 12 1988
but I get 25 Jan 1989
. Why?
我正在通过,25 12 1988
但我得到了25 Jan 1989
。为什么?
采纳答案by JB Nizet
Months are zero-based in Calendar. So 12 is interpreted as december + 1 month. Use
日历中的月份是从零开始的。所以 12 被解释为十二月 + 1 个月。用
c.set(year, month - 1, day, 0, 0);
回答by zw324
回答by Fdiazreal
Java's Calendar representation is not the best, they are working on it for Java 8. I would advise you to use Joda Timeor another similar library.
Java 的 Calendar 表示不是最好的,他们正在为 Java 8 工作。我建议您使用Joda Time或其他类似的库。
Here is a quick example using LocalDate from the Joda Time library:
这是一个使用 Joda Time 库中的 LocalDate 的快速示例:
LocalDate localDate = new LocalDate(year, month, day);
Date date = localDate.toDate();
Hereyou can follow a quick start tutorial.
在这里,您可以按照快速入门教程进行操作。
回答by Hajo Lemcke
Make your life easy when working with dates, timestamps and durations. Use HalDateTime from
处理日期、时间戳和持续时间,让您的生活更轻松。使用 HalDateTime 从
http://sourceforge.net/projects/haldatetime/?source=directory
http://sourceforge.net/projects/haldatetime/?source=directory
For example you can just use it to parse your input like this:
例如,您可以使用它来解析您的输入,如下所示:
HalDateTime mydate = HalDateTime.valueOf( "25.12.1988" );
System.out.println( mydate ); // will print in ISO format: 1988-12-25
You can also specify patterns for parsing and printing.
您还可以指定用于解析和打印的模式。
回答by Przemek
java.time
时间
Using java.time
framework built into Java 8
使用java.time
内置于 Java 8 的框架
int year = 2015;
int month = 12;
int day = 22;
LocalDate.of(year, month, day); //2015-12-22
LocalDate.parse("2015-12-22"); //2015-12-22
//with custom formatter
DateTimeFormatter.ofPattern formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate.parse("22-12-2015", formatter); //2015-12-22
If you need also information about time(hour,minute,second) use some conversion from LocalDate
to LocalDateTime
如果您还需要有关时间(小时,分钟,秒)的信息,请使用从LocalDate
到的一些转换LocalDateTime
LocalDate.parse("2015-12-22").atStartOfDay() //2015-12-22T00:00
回答by Fred Porciúncula
That's my favorite way prior to Java 8:
这是我在 Java 8 之前最喜欢的方式:
Date date = new GregorianCalendar(year, month - 1, day).getTime();
I'd say this is a cleaner approach than:
我会说这是一种比以下更清洁的方法:
calendar.set(year, month - 1, day, 0, 0);