从 java.util.Date 获取年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27489980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:27:28  来源:igfitidea点击:

get Year from java.util.Date

javacassandradeprecateddatastaxjava.util.date

提问by Chamila Wijayarathna

I have a date column in a Cassandra column family. When I retrieve data from this CF using datastax java API, this date object can be taken as a java.util.Dateobject.

我在 Cassandra 列族中有一个日期列。当我使用 datastax java API 从这个 CF 检索数据时,这个日期对象可以作为一个java.util.Date对象。

It has a getYear()method but it is deprecated. The corresponding javadoc says:

它有一个getYear()方法,但已被弃用。相应的 javadoc 说:

As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

从 JDK 1.1 版开始,由 Calendar.get(Calendar.YEAR) - 1900 取代。

How can I get the year, month, day attributes from this date object properly?

如何正确地从此日期对象获取年、月、日属性?

采纳答案by Semih Eker

Could you try like tihs;

你能试试吗?

      // create a calendar
      Calendar cal = Calendar.getInstance();
      cal.setTime(datetime);  //use java.util.Date object as arguement
      // get the value of all the calendar date fields.
      System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
      System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
      System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));

As mentioned in javadocs;

javadocs 中所述

@Deprecated public int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900. Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone. Returns: the year represented by this date, minus 1900.

@Deprecated public int getYear() 已弃用。从 JDK 版本 1.1 开始,由 Calendar.get(Calendar.YEAR) - 1900 替换。返回一个值,该值是从包含此 Date 对象表示的时刻或以该日期对象表示的时刻开始的年份减去 1900 的结果,如解释在当地时区。返回: 此日期表示的年份减去 1900。

回答by Rakesh KR

A good option is to use date format as follows:

一个不错的选择是使用日期格式,如下所示:

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
Date             date = sdf1.parse(datetime);
String           year = sdf2.format(date);

回答by Cyphrags

To retrieve the date & time fields you can use this code:

要检索日期和时间字段,您可以使用以下代码:

DateFormat.getDateTimeInstance().getCalendar().get(DateFormat.MONTH_FIELD)

Just replace MONTH_FIELD with one somehting else like "DAY_OF_WEEK_FIELD" to retrieve the day of the week (I think '1' stands for monday) or "MINUTE_FIELD" for the current minute, etc. :)

只需将 MONTH_FIELD 替换为“DAY_OF_WEEK_FIELD”之类的其他内容即可检索星期几(我认为“1”代表星期一)或当前分钟的“MINUTE_FIELD”等:)

回答by Ammar Bozorgvar

use LocalDate object in java8

在 java8 中使用 LocalDate 对象

Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year  = localDate.getYear();
int month = localDate.getMonthValue();
int day   = localDate.getDayOfMonth();