java date获取年月,我想从Java Date获取年,月,日等,以与Java中的公历日历进行比较。这可能吗?...

I have a Date object in Java stored as Java's Date type.

I also have a Gregorian Calendar created date. The gregorian calendar date has no parameters and therefore is an instance of today's date (and time?).

With the java date, I want to be able to get the year, month, day, hour, minute, and seconds from the java date type and compare the the gregoriancalendar date.

I saw that at the moment the Java date is stored as a long and the only methods available seem to just write the long as a formatted date string. Is there a way to access Year, month, day, etc?

I saw that the getYear(), getMonth(), etc. methods for Date class have been deprecated. I was wondering what's the best practice to use the Java Date instance I have with the GregorianCalendar date.

My end goal is to do a date calculation so that I can check that the Java date is within so many hours, minutes etc of today's date and time.

I'm still a newbie to Java and am getting a bit puzzled by this.

解决方案

Use something like:

Date date; // your date

Calendar cal = Calendar.getInstance();

cal.setTime(date);

int year = cal.get(Calendar.YEAR);

int month = cal.get(Calendar.MONTH);

int day = cal.get(Calendar.DAY_OF_MONTH);

// etc.

Beware, months start at 0, not 1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你好,根据您提供的信息,我可以使用Java编写代码来计算距离生还有多少天。以下是代码示例: ```java import java.time.LocalDate; import java.time.Month; import java.time.Period; public class CalculateDaysToBirthday { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate birthday = LocalDate.of(199, Month.JANUARY, 1); // 假设生是19911 LocalDate nextBirthday = birthday.withYear(today.getYear()); // 计算今的生期 if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) { nextBirthday = nextBirthday.plusYears(1); // 如果今的生已经过了,就计算明的生期 } Period period = Period.between(today, nextBirthday); int days = period.getDays(); System.out.println("距离生还有 " + days + " 天"); } } ``` 希望这个代码可以帮助您计算距离生还有多少天。 ### 回答2: 可以使用Java期类(Date)和日历类(Calendar)来计算距离生还有多少天。 首先,将已知的生期转换为Date类型的实例,可以使用SimpleDateFormat类来实现期的格式化转换。 ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class BirthdayCalculator { public static void main(String[] args) { // 已知的生期 String birthdayStr = "1990-08-15"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date birthday = null; try { birthday = dateFormat.parse(birthdayStr); } catch (Exception e) { e.printStackTrace(); } // 获取当前期 Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.set(Calendar.MILLISECOND, 0); // 获取期 Calendar birthDate = Calendar.getInstance(); birthDate.setTime(birthday); birthDate.set(Calendar.HOUR_OF_DAY, 0); birthDate.set(Calendar.MINUTE, 0); birthDate.set(Calendar.SECOND, 0); birthDate.set(Calendar.MILLISECOND, 0); // 计算距离生还有多少天 if (today.get(Calendar.DAY_OF_YEAR) > birthDate.get(Calendar.DAY_OF_YEAR)) { birthDate.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1); } else { birthDate.set(Calendar.YEAR, today.get(Calendar.YEAR)); } long diffMillis = birthDate.getTimeInMillis() - today.getTimeInMillis(); long diffDays = diffMillis / (24 * 60 * 60 * 1000); System.out.println("距离生还有 " + diffDays + " 天"); } } ``` 这段代码先将生期和当前期都设置为零点,然后通过比较当前期和生期,判断生是否已经过去。如果已经过去,则将生份设置为下一,否则设置为今。最后计算出两个期的毫秒差,除以一天的毫秒数得到相差的天数。 注意:该计算是基于公历期的简单计算,不考虑闰和其他历法的影响。在实际应用可能需要考虑更加精确的算法。 ### 回答3: 为了计算离生还有多少天,首先要知道当前的期和生期。可以使用Java期相关类来获取当前期,如LocalDate类。然后,将生期设置为所需的期并使用LocalDate类计算两个期之间的差距。 下面是一个简单的Java代码示例来计算离生还有多少天: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class CalculateDaysUntilBirthday { public static void main(String[] args) { // 获取当前期 LocalDate currentDate = LocalDate.now(); // 设置生期 LocalDate birthday = LocalDate.of(2022, 1, 1); // 假设生是202211 // 计算离生还有多少天 long daysUntilBirthday = ChronoUnit.DAYS.between(currentDate, birthday); System.out.println("离生还有 " + daysUntilBirthday + " 天"); } } ``` 在这个示例,首先获取当前期并将其存储在`currentDate`变量。然后,使用`LocalDate.of()`方法设置生期并将其存储在`birthday`变量。接下来,使用`ChronoUnit.DAYS.between()`方法计算当前期和生期之间的差距,并将结果存储在`daysUntilBirthday`变量。最后,将结果打印出来,即得到离生还有多少天。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值