Java – 将公历日历存储在变量中?

huangapple go评论68阅读模式
英文:

Java - Gregorian Calender Stored in variabe?

问题

我在尝试将此打印语句更改为可用于与其他变量进行比较的变量时遇到了困难。

是否有一种方法可以将输出存储为变量,而不是打印输出,以便在需要确定两个日期之间的差异时使用?

import java.util.GregorianCalendar;
import java.util.Calendar;

public class Test {

    public static void main(String[] args) {

        int Day = 8;
        int Month = 2;
        int Year = 1950;

        GregorianCalendar gcal = new GregorianCalendar(Year, Month, Day);

        String month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

        String formattedDate = month[gcal.get(Calendar.MONTH)] + " " + gcal.get(Calendar.DATE) + ", " + gcal.get(Calendar.YEAR);
        System.out.println(formattedDate);
    }
}

这个输出会打印:
Mar 8, 1950

英文:

im having a hard time trying to change this print statement into a variable for comparing with other variables.

Is there a way to store the output as a variable instead of printing incase you need to determine the difference of two dates?

import java.util.GregorianCalendar;
import java.util.Calendar;

public class Test {

public static void main(String[] args){

int Day = 8;
int Month = 2;
int Year = 1950;

    GregorianCalendar gcal = new GregorianCalendar(Year, Month, Day);

	String month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 

    System.out.println(month[gcal.get(Calendar.MONTH)] + " " + gcal.get(Calendar.DATE) + ", " 
    		+ gcal.get(Calendar.YEAR));}
}

This output prints:
Mar 8, 1950

答案1

得分: 2

存储为变量,以便与其他日期进行比较,调用getTime()

int Day = 8;
int Month = 2;
int Year = 1950;
GregorianCalendar gcal = new GregorianCalendar(Year, Month, Day);
Date date = gcal.getTime();

要将其格式化为Mar 8, 1950,请使用SimpleDateFormat

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy", Locale.US);
String str = dateFormat.format(date);

无需制作自己的月份名称。


然而,如果您使用的是 Java 8 或更高版本1,应该使用LocalDate类,而不是GregorianCalendar

int day = 8;
int month = 3;
int year = 1950;
LocalDate date = LocalDate.of(year, month, day);

请注意,与GregorianCalendar不同,month值基于1,因此它需要是3才能得到Mar

要将其格式化为Mar 8, 1950,请使用DateTimeFormatter

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MMM d, uuuu", Locale.US);
String str = date.format(dateFormat);

1) 如果您使用的是 Java 6 或 7,仍然可以通过添加ThreeTen-Backport库来使用LocalDate

英文:

To store as a variable, so it can be compared to other dates, call getTime():

int Day = 8;
int Month = 2;
int Year = 1950;
GregorianCalendar gcal = new GregorianCalendar(Year, Month, Day);
Date date = gcal.getTime();

To format that as Mar 8, 1950, use a SimpleDateFormat:

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy", Locale.US);
String str = dateFormat.format(date);

No need to make your own month names.


However, if you're using Java 8 or later<sup>1</sup>, you should use the LocalDate class instead of GregorianCalendar:

int day = 8;
int month = 3;
int year = 1950;
LocalDate date = LocalDate.of(year, month, day);

Note that unlike GregorianCalendar, the month value is 1-based, so it needs to be 3 to get Mar.

To format that as Mar 8, 1950, use a DateTimeFormatter:

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(&quot;MMM d, uuuu&quot;, Locale.US);
String str = date.format(dateFormat);

<sup>1) If you're using Java 6 or 7, you can still use LocalDate by adding the ThreeTen-Backport library.</sup>

huangapple
  • 本文由 发表于 2020年4月4日 05:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定