4

I need to get month and day as :01 for January, 02 for February... and 01 for first day of month, etc... I tried this :

 String dd = c.get(Calendar.YEAR) + "-" 
        + c.get(Calendar.MONTH)
        + "-" + c.get(Calendar.DAY_OF_MONTH) 
        + " " + c.get(Calendar.HOUR_OF_DAY) 
        + ":" + c.get(Calendar.MINUTE)
        + ":" + c.get((Calendar.SECOND));

but I get something like this :

2011-8-1 12:05:20

Which is the solution of this problem? For month I think I have to add 1,right?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gabrielle
  • 4,933
  • 13
  • 62
  • 122

6 Answers6

13
        String month=c.get(Calendar.MONTH)+1+"";
    if(month.length()<2){
         month="0"+month;   
    }  
Uroš Podkrižnik
  • 8,607
  • 5
  • 21
  • 31
11

You're over-complicating the problem. If all you need is to format a date in a certain way, then use java.text.SimpleDateFormat class. See the documentation here: http://developer.android.com/reference/java/text/SimpleDateFormat.html

If you really do need to get individual parts, then you are correct in using Calendar.get method. I suggest you read up on java.util.Calendar.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
7
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
String formattedDate = format.format(date);
B2arN
  • 93
  • 1
  • 5
1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.e("TEST",sdf.format(new Date()));
Yahia Mgarrech
  • 682
  • 9
  • 14
1

Try this to get date and time:

    Calendar rightNow = Calendar.getInstance();
    String y = String.valueOf(rightNow.getTime().getYear());
    String ym = String.valueOf(rightNow.getTime().getMonth());
    String yd = String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
    String h = String.valueOf(rightNow.getTime().getHours());
    String hm = String.valueOf(rightNow.getTime().getMinutes());
    String hs = String.valueOf(rightNow.getTime().getSeconds());
Nader MA
  • 117
  • 1
  • 5
0

tl;dr

ZonedDateTime.now( ZonedId.of( "America/Montreal" ) ).format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " )

Details

Which is the solution of this problem? For month I think I have to add 1,right?

Nope, not if you use better classes. You are using troublesome, confusing, poorly designed date-time classes which should be avoided. Instead use java.time classes.

ZonedDateTime

Get the current moment in your desired/expected time zone. Get a ZonedDateTime object.

ZoneId zoneId = ZonedId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( zoneId );

Interrogate for the parts if that is what you need. The java.time classes count month sensibly, 1-12 is January-December. By the way, check out the handy Month enum.

int month = zdt.getMonthValue();
int dayOfMonth = zdt.getDayOfMonth();

To get literally the string you showed, let the DateTimeFormatter class do the work. The predefined formatter DateTimeFormatter.ISO_LOCAL_DATE_TIME gets you nearly there. You can replace the T in the middle with a SPACE to finish.

DateTimeFormatter f = DateTimeFormatter.ISO_LOCAL_DATE_TIME ;
String output = zdt.format( f ).replace( "T" , " " );

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154