How can I get the current time and date in an Android app?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You can use the code:
Output:
You also get some more formatting options for
SimpleDateFormat
from here.Well I had problems with some answers by the API so I fuse this code, I hope it serves them guys:
Output: 03:25 PM - 2017/10/03
Below method will return current date and time in String, Use different time zone according to your actual time zone.I've used GMT
For a customized time and date format:
Output is like below format: 2015-06-18T10:15:56-05:00
tl;dr
…or…
Details
The other Answers, while correct, are outdated. The old date-time classes have proven to be poorly designed, confusing, and troublesome.
java.time
Those old classes have been supplanted by the java.time framework.
These new classes are inspired by the highly successful Joda-Time project, defined by JSR 310, and extended by the ThreeTen-Extra project.
See the Oracle Tutorial.
Instant
An
Instant
is a moment on the timeline in UTC with resolution up to nanoseconds.Time Zone
Apply a time zone (
ZoneId
) to get aZonedDateTime
. If you omit the time zone your JVM’s current default time zone is implicitly applied. Better to specify explicitly the desired/expected time zone.Use proper time zone names in the format of
continent/region
such asAmerica/Montreal
,Europe/Brussels
, orAsia/Kolkata
. Never use the 3-4 letter abbreviations such asEST
orIST
as they are neither standardized nor unique.Generating Strings
You can easily generate a
String
as a textual representation of the date-time value. You can go with a standard format, your own custom format, or an automatically localized format.ISO 8601
You can call the
toString
methods to get text formatted using the common and sensible ISO 8601 standard.Note that for
ZonedDateTime
, thetoString
method extends the ISO 8601 standard by appending the name of the time zone in square brackets. Extremely useful and important information, but not standard.Custom format
Or specify your own particular formatting pattern with the
DateTimeFormatter
class.Specify a
Locale
for a human language (English, French, etc.) to use in translating the name of day/month and also in defining cultural norms such as the order of year and month and date. Note thatLocale
has nothing to do with time zone.Localizing
Better yet, let java.time do the work of localizing automatically.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.