Print using Java a date and time from a MySql DATETIME field
I have a problem printing a date I get from a DATETIME field from my MySQL DB. I am trying to print a field with this DATETIME information: 2013-06-23 17:29:40
The format is YEAR-MONTH-DAY HOURS:MINUTES:SECONDS.
Priting the date I get from my DB result (cal.toString()): java.util.GregorianCalendar[time=1372001380000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2013,MONTH=5,WEEK_OF_YEAR=25,WEEK_OF_MONTH=3,DAY_OF_MONTH=23,DAY_OF_YEAR=174,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=29,SECOND=40,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=3600000]
I use Integer.toString to transform int attributes to String. However printing the year I get this: 1 Printing the month: 3 Printing the day: 5 Printing the hour: 11 Printing the minute: 12 Printing the seconds: 13
If I do this:
String month = Integer.toString(cal.get(Calendar.MONTH) + 1);
if (month.length() == 1)
month = "0" + month;
String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
if (day.length() == 1)
day = "0" + day;
String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY));
if (hour.length() == 1)
hour = "0" + hour;
String minute = Integer.toString(cal.get(Calendar.MINUTE));
if (minute.length() == 1)
minute = "0" + minute;
String second = Integer.toString(cal.get(Calendar.SECOND));
if (second.length() == 1)
second = "0" + second;
String date= cal.YEAR + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
I got this if I print the date string : 1-06-23 17:29:40
Does anybody know what is happening and how can I print the right date? Thanks in advance.
Once you got the Calendar
instance, you can get Date
instance from it, and formatted string with SimpleDateFormat
. For example,
Date d = cal.getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String s = df.format(d); // formatted date
The reason you got the wrong string is... cal.YEAR is static constance of Calendar
class and its value is defined as 1
.
String date= cal.YEAR + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
^^^^^^^^
That's why you got the wrong year.
If you should get each value of year, month, date, hour, minute, second, it can be done like this:
String year = String.format("%d", cal.get(Calendar.YEAR));
String month = String.format("%02d", cal.get(Calendar.MONTH)+1);
String date = String.format("%02d", cal.get(Calendar.DATE));
String hour = String.format("%02d", cal.get(Calendar.HOUR));
String minute = String.format("%02d", cal.get(Calendar.MINUTE));
String second = String.format("%02d", cal.get(Calendar.SECOND));
链接地址: http://www.djcxy.com/p/950.html
上一篇: 为什么AWST和CCT的时间不一样?