如何使用java从约会中获取小时数
从现在开始,以如何使用java从约会中获取小时数,java,time,date-format,simpledateformat,Java,Time,Date Format,Simpledateformat,从现在开始,以12小时格式仅获取小时的日期格式是什么 Thu Oct 20 13:12:00 GMT+02:00 2011 编辑: 使用此代码 Date eventDate = tempAppointments.get(i).mStartDate System.out.println(eventDate.toString()); // date pattern DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed
12小时格式仅获取小时的日期格式是什么
Thu Oct 20 13:12:00 GMT+02:00 2011
编辑:
使用此代码
Date eventDate = tempAppointments.get(i).mStartDate
System.out.println(eventDate.toString());
// date pattern
DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed Nov 09 11:00:00 GMT+02:00 2011
// get the start date with new format (pattern)
String hours = df.format(tempAppointments.get(i).mStartDate.getDay());
System.out.print(hours);//output: 02:00 AM
返回时间为
02:00 AM
但在给定的时间内。必须是下午2:00
。为什么?如果您想要小时部分,我不确定为什么要将date.getDay()
(顺便说一句,它已被弃用)传递到格式化程序中
试试这个:-
Date date = new Date();
System.out.println("Date: " + date);
DateFormat df = new SimpleDateFormat("hh:'00' a");
String hour = df.format(date);
System.out.println("Hour: " + hour);
输出:
Date: Sat Nov 19 17:57:05 CST 2011
Hour: 05:00 PM
如果您能够提供完整的、运行中的代码来演示问题,那么诊断问题就会容易得多——问题是,它为什么要编译?我看不到任何DateFormat#format(int)的格式,那么给出了什么呢?您正在格式化date对象,而不是int。请看这里的API:是的,问题是date#getDay()返回int(在原始代码中是),即使df.format(1)编译也返回int。从int到现在是否有一些隐式转换?您应该指出这解决问题的原因。
Date fecha = new Date();
System.out.println("Fecha "+fecha);
DateFormat formato = new SimpleDateFormat("hh:mm:ss");
String hora = formato.format(fecha);
System.out.println("Son las "+hora);