TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Storing Month Format on a Database (From Java)

I have an Android app where there is a spinner (drop-down) list where I list the next 24 months from the current date. (The Context: The user can pick a date in the next two years to schedule a payment).

Currently, I correctly display the current date (based by locale) in the spinner and this is not the problem. My question is: What kind of unit should I store for the value of the selection? The first choice seemed like a unix timestamp, but that refers to a specific point in time down to the millisecond and doesn't represent a whole month. I can't just use a month (for example, 10 is November), because I need the year too.

For example, it is September 2014 as I write this. If the user wants to schedule a payment two months from now, they would select "Nov 2014" (the format I choose). What value can I use in the database that would ALWAYS represent this month in this specific year?

I fear I am overthinking this.

Here is my code so far:

for (int i = 0; i < 24;i++) {

        Month m = new Month();
        cal.add(Calendar.MONTH, i);
        Date date = cal.getTime();

        m.num = date.toString(); // This is where I am trying to store the output - not sure if I am on right track?

   }

Month is my object I am creating for the spinner. It has two values; one not shown here that is the readable format, the other is num which is my value.

Upvotes: 1

Views: 141

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339382

The answer by trooper is correct and good. Using a string in the format yyyy-MM is smart:

  • Readable
  • Sorting alphabetically is also sorting chronologically
  • Complies with the sensible ISO 8601 standard for date-time strings.

Joda-Time

I'm just adding a bit of example code showing how to use Joda-Time 2.4 library to make use of the YearMonth class to represent this year+month meaning, and parse/generate strings.

String input = "2014-02";
YearMonth yearMonth = YearMonth.parse( input ); // Construct by parsing string.
int year = yearMonth.getYear(); 
int month = yearMonth.getMonthOfYear();
String output = yearMonth.toString(); // Joda-Time uses ISO 8601 formats by default for generating strings.
YearMonth yearMonth2 = new YearMonth( year , month ); // Construct by passing numbers for year and month.

Dump to console.

System.out.println( "input: " + input );
System.out.println( "yearMonth: " + yearMonth );
System.out.println( "year: " + year );
System.out.println( "month: " + month );
System.out.println( "output: " + output );
System.out.println( "yearMonth2: " + yearMonth2 );

When run.

input: 2014-02
yearMonth: 2014-02
year: 2014
month: 2
output: 2014-02
yearMonth2: 2014-02

Upvotes: 1

trooper
trooper

Reputation: 4512

Don't overthink it. Store your date in the database as a varchar using a specific string format. I would represent dates using "yyyy-MM", as this makes it easy to conveniently sort rows by date.

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM");

// from date to string
String dateString = dateFormatter.format(new Date()); 

// and back
Date date = dateFormatter.parse(dateString);

Upvotes: 2

Related Questions