SimpleDateFormat android not formatting as expected

风格不统一 提交于 2019-12-11 08:53:33

问题


I'm trying to use SimpleDateFormat for formatting a date represented by 3 ints. It looks like this:

...
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");

Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getDefault());
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);

String string_hours = sdfHour.format(hours);
String string_minutes = sdfMinute.format(minutes);
String string_seconds = sdfSecond.format(seconds);

and the output of

Log.d("tag", "Time string is: " + string_hours + ":" + string_minutes + ":" + string_seconds);

is always

Time string is: 19:00:00

What am I doing wrong here?


回答1:


SimpleDateFormat.format expects a Date, not an int. The method you're using, which is the overloaded version that accepts a long, is actually expecting milliseconds from the epoch, not an hour a minute or a second as you're doing.

The right way of using it should be :

SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
String timeString = sdfHour.format(new Date());

Using "new Date()" as in this example, will give you the current time. If you need to format some other time (like one hour ago, or something from a database etc..) pass to "format" the right Date instance.

If you need the separated, for some reason, then you can still use it, but this other way :

SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");

Date now = new Date();

String string_hours = sdfHour.format(now);
String string_minutes = sdfMinute.format(now);
String string_seconds = sdfSecond.format(now);



回答2:


Try something like this:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());



回答3:


You are calling wrong format method. You should supply a Date argument to a proper one, instead you are using this one, inherited from Format class:

public final String format(Object obj)

Why does it work? Because of auto-boxing procedure in Java. You provide an int, it's automatically boxed to Integer which is a successor of Object




回答4:


You can't use SimpleDateFormat like this:

SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");

Use this:

long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String dateformatted = dateFormat.format(cal1.getTime());

refer this



来源:https://stackoverflow.com/questions/25972040/simpledateformat-android-not-formatting-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!