java 如何在Android中获取当前时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35579464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current time in Android?
提问by Roni Zaitsev
I need to get the time when the user is touching a button. like, when the user is touching button1, I need it to save the exact hour, minute and date of touching this button. how can I do it?
我需要获取用户触摸按钮的时间。例如,当用户触摸 button1 时,我需要它来保存触摸此按钮的确切小时、分钟和日期。我该怎么做?
回答by Kanchan Chowdhury
You can try the following code:
您可以尝试以下代码:
Calendar calendar = Calendar.getInstance(Locale.getDefault());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int date = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
回答by chilltouch
System.currentTimeMillis()
You can use this function to escape creation of date object.
您可以使用此函数来逃避日期对象的创建。
You can persist it as long and you always can create Date object of it.
你可以坚持多久,你总是可以创建它的 Date 对象。