i'm trying to get the current date and time formatted, this is my code:
Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);
Something is going wrong because i'm getting this string: 19700103-0217
That date is incorrect, I should get something like this: 20111118-1217
How to solve this problem?
To get Current Date. Try this
The problem is that your date is incorrect. It's hard to say how, as you haven't shown where it's coming from, but that's pretty much bound to be the problem. To confirm this, use the built-in
Date.toString()
method - that uses the system default time zone, so you need to be careful, but you should at least see the right date etc.//This will get you the current month.
Date date = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("MMM");
//This will get you the current date of the month.
SimpleDateFormat format1 = new SimpleDateFormat("dd"); String currentDate = format1.format(date);
If you need other formats, you can see this link: http://developer.android.com/reference/java/text/SimpleDateFormat.html
If your goal is just to show current date then you can use
Calendar
though.Using calendar,its pretty easy to get the date in desired format.
Ex:
For more formates,refer this.
I would say the device (or emulator) on which you execute your code has an incorrect date. Your code should work as expected.
In any case, it's not related to
SimpleDateFormat
, ythe way you use it is correct.Well, I can help you out but I have my own code. Ask me if you don't get something.
First of all you have to make a class of any name like date.class and write the code:
After making the class, you have to declare this:
Now find the edit box in which you want to display the date like:
Now you have to display date on edit box as follows:
I hope you got my answer.