java-如何保持时间?
我正在构建一个小的android应用程序,用户可以在其中选择一些操作的开始时间 我正在使用TimerPickerDialog:java-如何保持时间?,java,android,time,java-time,Java,Android,Time,Java Time,我正在构建一个小的android应用程序,用户可以在其中选择一些操作的开始时间 我正在使用TimerPickerDialog: mTimePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedM
mTimePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
startTimeHours = selectedHour;
startTimeMinutes = selectedMinute;
}, hour, minute, is24hours);
当选择一个时间时,它返回一个int表示小时,int表示分钟,我将其保存为全局变量
稍后在我的应用程序中,我想将这些数字转换成一个时间对象,然后我可以使用它来查看当前时间是大于还是小于该类中表示的时间
我所做的是:
Time tStart = new Time(startTimeHours, startTimeMinutes, 0);
但是eclips说这个构造函数已经被弃用了。然而,花费很长时间的构造函数似乎并没有被弃用
我环顾四周,在一篇帖子中,有人建议使用Calendar对象——但我想不出来——如何获取它的新实例并设置我的时间。它看起来像是一个只包含时间的开销
对于这种情况,最佳做法是什么?如果需要使用接受长变量的构造函数-如何将2个整数转换为该长值?EDIT2:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
这就是创建两个不同时间的Date
对象并在之后进行比较的方法:
Calendar calendar = Calendar.getInstance(); // By default the date and time is set to now
// Create first date
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
Date dateOne = calendar.getTime();
// Create second date
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 36);
Date dateTwo = calendar.getTime();
// With before() you can check if a Date is before some other date
if(dateOne.before(dateTwo)) {
// dateOne is before dateTwo
} else {
// dateTwo is before dateOne
}
您还可以使用after()
检查某些Date
是否在其他Date
之后
编辑:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
你在问题最后一段提到的Long
值是以毫秒为单位的历元时间。您确实不应该使用框架中的Time
类。我想你是说?你应该使用我在下面发布的解决方案
原始答案:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
我通常倾向于使用Date
对象作为容器。创建Date
实例的最佳实践是使用Calendar
对象。试着这样做:
// We get a Calendar instance with getInstance();
Calendar calendar = Calendar.getInstance();
// Now we set the date we want.
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 4); // Be carefull, months start at 0. January = 0, Feburary = 1, March = 2,...
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
// And finally we create the `Date` instance with getTime()
Date date = calendar.getTime();
// You can also do the reverse and set the time in a `Calendar` instance from a `Date` instance.
calendar.setTime(date);
public class Time {
private int minute;
private int hour;
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
Date newDate = new DateBuilder().hour(10).minute(20).build();
这非常简单,Calendar
功能非常强大,但我认为这不适合您。因为此解决方案创建了一个Date
对象。因此,它存储的不仅仅是小时和分钟
你必须决定这是否适用于你的情况。如果您真的只想存储小时和分钟,我建议您创建一个自定义的Time
类,如下所示:
// We get a Calendar instance with getInstance();
Calendar calendar = Calendar.getInstance();
// Now we set the date we want.
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 4); // Be carefull, months start at 0. January = 0, Feburary = 1, March = 2,...
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
// And finally we create the `Date` instance with getTime()
Date date = calendar.getTime();
// You can also do the reverse and set the time in a `Calendar` instance from a `Date` instance.
calendar.setTime(date);
public class Time {
private int minute;
private int hour;
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
Date newDate = new DateBuilder().hour(10).minute(20).build();
如有必要,您还可以将验证和计算添加到此Time
类中,但这同样取决于您希望执行的操作
如果您向我们提供有关您需要存储何种时间值的更多信息,如果还需要存储任何日期信息,我们可以为您提供更准确的答案。EDIT2:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
这就是创建两个不同时间的Date
对象并在之后进行比较的方法:
Calendar calendar = Calendar.getInstance(); // By default the date and time is set to now
// Create first date
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
Date dateOne = calendar.getTime();
// Create second date
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 36);
Date dateTwo = calendar.getTime();
// With before() you can check if a Date is before some other date
if(dateOne.before(dateTwo)) {
// dateOne is before dateTwo
} else {
// dateTwo is before dateOne
}
您还可以使用after()
检查某些Date
是否在其他Date
之后
编辑:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
你在问题最后一段提到的Long
值是以毫秒为单位的历元时间。您确实不应该使用框架中的Time
类。我想你是说?你应该使用我在下面发布的解决方案
原始答案:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Sample startTimeHours and startTimeMinutes (to be obtained from
// TimerPickerDialog in your case
int startTimeHours = 19;
int startTimeMinutes = 0;
LocalTime startTime = LocalTime.of(startTimeHours, startTimeMinutes);
LocalTime now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
System.out.println(
now.isAfter(startTime) ? "Past" :
now.isBefore(startTime) ? "Future" :
"Present"
);
}
}
Present
我通常倾向于使用Date
对象作为容器。创建Date
实例的最佳实践是使用Calendar
对象。试着这样做:
// We get a Calendar instance with getInstance();
Calendar calendar = Calendar.getInstance();
// Now we set the date we want.
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 4); // Be carefull, months start at 0. January = 0, Feburary = 1, March = 2,...
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
// And finally we create the `Date` instance with getTime()
Date date = calendar.getTime();
// You can also do the reverse and set the time in a `Calendar` instance from a `Date` instance.
calendar.setTime(date);
public class Time {
private int minute;
private int hour;
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
Date newDate = new DateBuilder().hour(10).minute(20).build();
这非常简单,Calendar
功能非常强大,但我认为这不适合您。因为此解决方案创建了一个Date
对象。因此,它存储的不仅仅是小时和分钟
你必须决定这是否适用于你的情况。如果您真的只想存储小时和分钟,我建议您创建一个自定义的Time
类,如下所示:
// We get a Calendar instance with getInstance();
Calendar calendar = Calendar.getInstance();
// Now we set the date we want.
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 4); // Be carefull, months start at 0. January = 0, Feburary = 1, March = 2,...
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
// And finally we create the `Date` instance with getTime()
Date date = calendar.getTime();
// You can also do the reverse and set the time in a `Calendar` instance from a `Date` instance.
calendar.setTime(date);
public class Time {
private int minute;
private int hour;
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
Date newDate = new DateBuilder().hour(10).minute(20).build();
如有必要,您还可以将验证和计算添加到此Time
类中,但这同样取决于您希望执行的操作
如果您向我们提供更多关于您需要存储什么类型的时间值的信息,我们可以给您一个更准确的答案,更准确地说,如果需要存储任何日期信息,我们可以给您一个更准确的答案。而不是花时间创建使用有限的类,比如时间对象,它除了包含日期的上下文之外,实际上没有任何用途,专注于使日期更容易操作 在日历上使用set方法是一种严重的拖累,而且很难看。在Java8中,日期/时间的部分原因就是这个 也就是说,我不久前有点厌倦了,制作了一个DateBuilder,它使用fluent builder设计模式,让您可以使用以下方法轻松构建日期:
// We get a Calendar instance with getInstance();
Calendar calendar = Calendar.getInstance();
// Now we set the date we want.
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 4); // Be carefull, months start at 0. January = 0, Feburary = 1, March = 2,...
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 5);
// And finally we create the `Date` instance with getTime()
Date date = calendar.getTime();
// You can also do the reverse and set the time in a `Calendar` instance from a `Date` instance.
calendar.setTime(date);
public class Time {
private int minute;
private int hour;
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
Date newDate = new DateBuilder().hour(10).minute(20).build();
最后,我增加了一些其他的方便,比如给我午夜时间,或者让我增加时间。Java中日期工作方式最糟糕的一点是,您发现自己编写了大量代码,这很难看,并且大大降低了周围代码的可读性。建筑商将其划分
让人惊讶的是,跳入自己的类并添加类似add函数的内容是多么容易,例如:
new DateBuilder(new Date()).addDays(3).build();
我在8楼玩了新的约会工具,感觉更好。Joda也很不错,但有趣的是,最终我还是不太喜欢Joda,因为虽然Joda很不错,但它只是在代码中加入了一些相同的东西,而且它没有通过你采用的库中最重要的测试之一:每次使用它时,我都不得不回去阅读文档。在Objective-C中使用Categories让我确信,通常最好是自己动手设计代码的顶层,部分原因是你更可能制作一些流畅使用的东西(因此得名),但也因为它是一种准DSL:如果我在不同的领域中编程,我可能想要一个不同的DateBuilder,它强调其他事情。而不是花时间制作有限的类