Android时间结构
我有一个类,我想存储特定的时间。时间将用于安排要执行的任务。现在我正在使用android.text.format中的Android时间结构,android,datetime,time,Android,Datetime,Time,我有一个类,我想存储特定的时间。时间将用于安排要执行的任务。现在我正在使用android.text.format中的Time类,但这并不是我想要的 我可以创建时间戳,但它会填充日期、时区等 Time startTime = new Time(); startTime.setToNow(); tvConsole.setText(startTime.toString()); // just to view it in the emulator 结果是: 20101208T162807Ameri
Time
类,但这并不是我想要的
我可以创建时间戳,但它会填充日期、时区等
Time startTime = new Time();
startTime.setToNow();
tvConsole.setText(startTime.toString()); // just to view it in the emulator
结果是:
20101208T162807America/New_York(3,341,-18000,0,1291843687)
我不需要所有这些信息,也不想要它。有没有办法访问hh/mm/ss成员?我知道我可以只设置对象的时间,但它显示为1970年的某个日期。。。而且我不能只检索时间成员。有没有其他我不知道可以用的课程
感谢使用时间字段:
Time startTime = new Time();
startTime.setToNow();
startTime.hour
startTime.minute
startTime.second
如果您只需要一个可显示的时间(如“1:59pm”),那么可以使用DateUtils
//Look at the different FORMAT_ flags on DateUtils.
String s = DateUtils.formatDateTime(this, startTime.toMillis(true),
DateUtils.FORMAT_SHOW_TIME);
使用时间字段:
Time startTime = new Time();
startTime.setToNow();
startTime.hour
startTime.minute
startTime.second
如果您只需要一个可显示的时间(如“1:59pm”),那么可以使用DateUtils
//Look at the different FORMAT_ flags on DateUtils.
String s = DateUtils.formatDateTime(this, startTime.toMillis(true),
DateUtils.FORMAT_SHOW_TIME);
四处看看后,我找到了我要找的东西。答案是
日历
类。我在下面提供了一个代码示例
DateFormat formatTime = DateFormat.getTimeInstance(); // set the format type
Calendar startTime = Calendar.getInstance(); // this line will store the current time
startTime.set(Calendar.HOUR_OF_DAY, 10); // modify the time to something
startTime.set(Calendar.MINUTE, 30);
startTime.set(Calendar.SECOND, 40);
if (startTime.get(Calendar.HOUR_OF_DAY == 10) // example of accessing data member
tvConsole.setText(formatTime.format(startTime.getTime())); // display
这将显示:
10:30:40 PM
在四处查看之后,我找到了我要找的东西。答案是日历
类。我在下面提供了一个代码示例
DateFormat formatTime = DateFormat.getTimeInstance(); // set the format type
Calendar startTime = Calendar.getInstance(); // this line will store the current time
startTime.set(Calendar.HOUR_OF_DAY, 10); // modify the time to something
startTime.set(Calendar.MINUTE, 30);
startTime.set(Calendar.SECOND, 40);
if (startTime.get(Calendar.HOUR_OF_DAY == 10) // example of accessing data member
tvConsole.setText(formatTime.format(startTime.getTime())); // display
这将显示:
10:30:40 PM
您也可以对时间使用本机格式:
Time startTime = new Time();
startTime.setToNow();
tvConsole.setText(startTime.format("%I:%M %P")); // just to view it in the emulator
您可以使用的所有变量都列在这里:您也可以使用本机时间格式:
Time startTime = new Time();
startTime.setToNow();
tvConsole.setText(startTime.format("%I:%M %P")); // just to view it in the emulator
这里列出了您可以使用的所有变量:不能这样做
startTime.hour
不是检索hour数据成员的方法。它从API级别3开始就存在。实际上它只是一个公共int变量,不是一个方法。不能这样做startTime.hour
不是检索hour数据成员的方法。它从API级别3开始就存在。实际上它只是一个公共int变量,不是一个方法。