2

I hate to admit this, but I'm having trouble making sense of basic date formatting. I know that I am simply over-thinking this, but the documentation that I am seeing is just missing me completely. What I am trying to do is simply display the current date formatted as "Wednesday, February 20, 2013" for example. What I have so far is:

TextView textView = (TextView) findViewById(R.id.currentDate);
String now = DateFormat.getDateInstance().format(new Date());
textView.setText(now); 

This produces something like "2013-2-20", which is a great first step, but not what I want. In looking through the documentation I am confused because the DateFormat docs all say to use an integer to signify SHORT, MEDIUM, LONG, or DEFAULT. What? Where, exactly, would I put this argument? And how / which integer signifies SHORT vs LONG? None of this makes sense and I feel like a complete moron because I know it is very obvious. I'm coming at this from years of using PHP's date formatting and I am sure that my old habits are completely blocking me from seeing the simplicity of this.

Blind Fish
  • 998
  • 4
  • 14
  • 25
  • 1
    Take a look at this blog on formatting date http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/ – Jonathan Drapeau Feb 20 '13 at 21:51
  • 4
    The key concept that you're missing is that the original implementation of date formatting is hideously broken in Java; that the Java replacement for the original implementation is broken even more horribly; and that the Android helper utilities that attempt to make the original broken implementations usable aren't consistent in which family of APIs they use. If you find it difficult, it's because nothing about date format in Java is, or ever will be easy. Just so you know. – Robin Davies Feb 20 '13 at 21:55
  • @Johnathan: This is where I was getting completely confused. In the blog you referenced, which I think I may have read earlier, I find things such as "System.out.println(" 8. " + DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT).format(now)); And I am completely thrown by the "8". – Blind Fish Feb 20 '13 at 22:42
  • @Robin: That makes me feel a bit better though I must admit that the problem today is just me not possessing the intellectual capacity of a sock full of rusty doorknobs. – Blind Fish Feb 20 '13 at 22:44
  • Was my answer below acceptable? If so, please accept as answer. If not, please let me know what I can do to clarify or improve it for you. – Deep in the Code Mar 01 '13 at 16:09

2 Answers2

3

You would put the attribute (SHORT, LONG, etc.) inside the DateFormat.getDateInstance(); like this:

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

SHORT is completely numeric, such as 12.13.52 or 3:30pm

MEDIUM is longer, such as Jan 12, 1952

LONG is longer, such as January 12, 1952 or 3:30:32pm

FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

This information can be found here.

Deep in the Code
  • 572
  • 1
  • 6
  • 20
  • 1
    Thank you, David. I swear to god I read that page at least eight times today and did not see that. I'm going on about 2 hours sleep and battling the flu. Thank you taking the time to point out that. – Blind Fish Feb 20 '13 at 22:41
  • Ok, so this is still making little sense. I changed my original to String now = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()); And it had absolutely no effect. The returned string was exactly the same as it was prior. What am I missing here? – Blind Fish Feb 20 '13 at 23:00
  • I'm not sure; I wrote a short Java program that I got to work; I'll post it. – Deep in the Code Feb 21 '13 at 20:40
  • 'import java.util.Date; import java.text.DateFormat; public class test { public static void main(String args[]) { String now = DateFormat.getDateInstance(DateFormat.SHORT).format(new Date()); System.out.println("SHORT:" + now); now = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()); System.out.println("MEDIUM:" + now); now = DateFormat.getDateInstance(DateFormat.LONG).format(new Date()); System.out.println("LONG:" + now); now = DateFormat.getDateInstance(DateFormat.FULL).format(new Date()); System.out.println("FULL:" + now); } }' – Deep in the Code Feb 21 '13 at 20:42
  • The result was: SHORT:2/21/13 MEDIUM:Feb 21, 2013 LONG:February 21, 2013 FULL:Thursday, February 21, 2013 – Deep in the Code Feb 21 '13 at 20:42
1

If you're used to conventional languages, the key concept that you're missing is that date/time handling in Java is hideously broken. Not just once, but twice, in two separate incarnations of broken. Plus a few after-the-fact band-aid helper classes such as DateUtils. The key insight you need to come to terms with is: nothing to do with dates and times in Java is EVER simple.

Try using SimpleDateFormat instead. No it isn't actually simple. But the default settings produce a workable localized date/time (if not exactly a beautiful one).

    DateFormat format = SimpleDateFormat.getDateTimeInstance(),

while produces: "Dec 31, 1969 4:00:00 PM" (or localized equivalent).

Robin Davies
  • 7,547
  • 1
  • 35
  • 50
  • I'm starting to believe you. However, I'm really just looking for a simple "Tuesday, March 6, 1854" or whatever. This is so incredibly simple in language such as Php or Python. – Blind Fish Feb 20 '13 at 23:02