1

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

null.ts
  • 33
  • 1
  • 6
  • I recommend you don’t use `GregorianCalendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). I also recommend you don’t hardcode the abbreviations of the names of the months yourself. These abbreviations are already built into Java (in many languages). Use a `DateTimeFormatter`, – Ole V.V. Apr 04 '20 at 04:18
  • For determining the difference between two dates see for example [Calculate days between two dates in Java 8](https://stackoverflow.com/questions/27005861/calculate-days-between-two-dates-in-java-8). Search for more. – Ole V.V. Apr 04 '20 at 04:20

1 Answers1

2

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 later1, 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("MMM d, uuuu", Locale.US);
String str = date.format(dateFormat);

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

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Hi, Thanks for your response i appreciate it. Is it possible to compare two dates within the format of localDate? – null.ts Apr 03 '20 at 22:40
  • 1
    @null.ts `LocalDate` is a `Comparable`, so it has `compareTo(other)`. It also has `isAfter(other)`, `isBefore(other)`, and `isEqual(other)`. --- You could easily have seen this for yourself, if you had clicked the link on the [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) in the answer. – Andreas Apr 03 '20 at 22:49
  • @null.ts To reduce the confusion about month numbering further, you may use `Month month = Month.MARCH` instead of the `int` (`Month` is a built-in enum) (there’s also a constant that you could have used with `GregorianCalendar`, but I take it from your comment that you don’t need that). – Ole V.V. Apr 04 '20 at 04:24