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?
String month=c.get(Calendar.MONTH)+1+"";
if(month.length()<2){
month="0"+month;
}
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
.
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
String formattedDate = format.format(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.e("TEST",sdf.format(new Date()));
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());
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.
精彩评论