Java SimpleDataFormat android未按预期格式化

Java SimpleDataFormat android未按预期格式化,java,android,simpledateformat,Java,Android,Simpledateformat,我正在尝试使用SimpleDataFormat格式化由3个整数表示的日期。 看起来是这样的: ... SimpleDateFormat sdfHour = new SimpleDateFormat("HH"); SimpleDateFormat sdfMinute = new SimpleDateFormat("mm"); SimpleDateFormat sdfSecond = new SimpleDateFormat("ss"); Calendar c = Calendar.getInst

我正在尝试使用SimpleDataFormat格式化由3个整数表示的日期。 看起来是这样的:

...
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);
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());
以及

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

Time string is: 19:00:00

这里我做错了什么?

SimpleDataFormat.format需要一个日期,而不是一个int。您使用的方法是重载版本,它接受一个long,实际上是从纪元开始的毫秒,而不是一小时一分钟或一秒钟

正确的使用方法应该是:

SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
String timeString = sdfHour.format(new Date());
在本例中使用“new Date()”将为您提供当前时间。如果需要格式化其他时间(如一小时前,或数据库中的某个时间等),请传递“格式化”正确的日期实例

如果出于某种原因,您需要分开,那么您仍然可以使用它,但另一种方式是:

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);

SimpleDataFormat.format需要一个日期,而不是一个整数。您使用的方法是一个重载版本,它接受一个长的,实际上是从纪元开始的毫秒,而不是像您这样的一小时一分钟或一秒钟

正确的使用方法应该是:

SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
String timeString = sdfHour.format(new Date());
在本例中使用“new Date()”将为您提供当前时间。如果需要格式化其他时间(如一小时前,或数据库中的某个时间等),请传递“格式化”正确的日期实例

如果出于某种原因,您需要分开,那么您仍然可以使用它,但另一种方式是:

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);

不能像这样使用
SimpleDataFormat

...
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);
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());
使用以下命令:

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

请参阅您不能像这样使用
SimpleDataFormat

...
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);
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());
使用以下命令:

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

请参考以下内容:

...
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);
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());

试着这样做:

...
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);
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
SimpleDateFormat sdfSecond = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String CurrentTime = sdf.format(cal.getTime());

您调用了错误的
format
方法。您应该为适当的参数提供一个
Date
参数,而不是使用继承自
Format
class的参数:

public final String format(Object obj)

它为什么有效?因为Java中的自动装箱过程。您提供了一个
int
,它将自动装箱为
Integer
,它是调用错误的
格式方法的
对象的后续对象。您应该为适当的参数提供一个
Date
参数,而不是使用继承自
Format
class的参数:

public final String format(Object obj)

它为什么有效?因为Java中的自动装箱过程。您提供了一个
int
,它将自动装箱为
整数
,该整数是
对象的继承者

您期望的是什么?期望的是什么?希望这对您有所帮助!希望这对你有帮助!