1

I'm trying to get a short month name by passing the month int to a function. But it always returns 'Jan'

Here is the function:

public static String getMonthName_Abbr(int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month);
    SimpleDateFormat month_date = new SimpleDateFormat("MMM");
    String month_name = month_date.format(month);
    return month_name; 
}
DaveSav
  • 1,364
  • 5
  • 21
  • 41
  • Something is strange here. According to the [docs for SimpleDateFormat](http://developer.android.com/reference/java/text/SimpleDateFormat.html), the format method requires a `Date` and you passed it an `int`. – Ray Toal Dec 23 '12 at 01:10
  • I don't know. I was expecting some magic to happen. That code block is just my latest attempt at trying to implement the answer given at http://stackoverflow.com/questions/6192781/how-to-get-month-as-a-string-in-android – DaveSav Dec 23 '12 at 01:14
  • Oh I see, pass `cal.getTime()` to the `format` method. – Ray Toal Dec 23 '12 at 01:16
  • Thank you, Ray. That has worked. My app is for api 8 and above, but I only have a real api 8 device to work with; would your solution work with higher api's ? – DaveSav Dec 23 '12 at 01:24
  • Yes, this will work with API 8, as `Calendar` and `SimpleDateFormat` are plain Java and have been there since the beginning. The differences between 7 and 8 are [here](http://developer.android.com/sdk/api_diff/8/changes.html) -- there are no changes in any of the classes used in your example. – Ray Toal Dec 23 '12 at 01:49

3 Answers3

2

You simply need to pass cal.getTime() to your format call, rather than month itself.

See the working demo at http://ideone.com/kKrGY9

I am not sure why your code worked as given, seeing as how format expects a Date and not an int; however, if somehow it did, perhaps the value of month, being a small integer was interpreted as a time around the epoch (January 1, 1970). Just a thought, but at any rate, your function will work with that one small change.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Yeah, the first 31 * 24 * 60 * 60 * 1000 milliseconds would be timestamps that are in the month of January, and he was passing values from 0 to 11. So straight into the first second of Jan 1, 1970. Funny bug, really :D – Davor Jul 14 '13 at 16:04
0
public static String getMonthName_Abbr(int month) {
    final String[] allMonths = DateFormatSymbols.getInstance(Locale.getDefault()).getShortMonths();
    return (month >= 0 && month < allMonths.length) ? allMonths[months] : "Invalid month";
}
zen_of_kermit
  • 1,404
  • 2
  • 13
  • 19
0

UPDATE ::: The functions could be even further simplified to just ::


function  longMonth(_) { return _%2~_<8 }

1 3 5 7 8 10 12

function shortMonth(_) { return _%2~7<_ }

2 4 6 9 11

if you already have the month number (1-12), here's an easy way to determine whether it's a short- or long-month without regex too much hard-coding :

jot 12 | {m,g,n}awk '
function shortMonth_v1(_) { return \
                                   (_ % 2) != (+_ < 8) }
function shortMonth_v2(_) { return \
                                   (_ + (+_ < 8)) % 2  }'
2
4
6
9
11
function  longMonth_v1(_) { return \
                                   (_ % 2) == (+_ < 8) }
function  longMonth_v2(_) { return \
                                   (_ + (7 < +_)) % 2  }
1
3
5
7
8
10
12

...v1() and ...v2() for both groups basically identical, reshuffling things around, so which one you prefer is just a matter of taste and personal preference

assuming if you really wanna match it by string-regex of first-3 letters, June is by far the hardest on

 tolower($0) ~ /[fpv]|un/
/[bpv]|un/    # these 2 work as long as the input isn't all-caps
/[bpo]|un/
/^F|p|un|v/   # assuming first letter capitalized, others lower-case
/[FNp]|un/
Fe[b]    b             # can't use [e] to match
                       # since that'll grab in D[e]cember
A[p]r       p
J[u][n]          un
Se[p]       p
No[v]          v
/[acgl]/            # my favorite, by far
J[a]n   a
M[a]r   a
M[a]y   a
Ju[l]              l
Au[g]          g
O[c]t      c  
De[c]      c
/^M|an|[cgl]/       # alternatives
/[Mc]|an|u[^n]/
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11