Time time = new Time(); time.setToNow();Log.d("TIME TEST", Long.toString(time.toMillis(false)));... do something that takes more than one millisecond, but less than one second ...
Time today = new Time(Time.getCurrentTimezone());today.setToNow();
然后,您可以获取所需的所有日期字段,例如:
textViewDay.setText(today.monthDay + ""); // Day of the month (1-31)textViewMonth.setText(today.month + ""); // Month (0-11)textViewYear.setText(today.year + ""); // YeartextViewTime.setText(today.format("%k:%M:%S")); // Current time
Date date = new Date(System.currentTimeMillis());SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",Locale.ENGLISH);String var = dateFormat.format(date));
formatter = formatter.withLocale( Locale.US ); // Or Locale.CANADA_FRENCH or such.String output = zdt.format( formatter );
本地化
更好的是,让java.time自动完成本地化工作。
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM );String output = zdt.format( formatter.withLocale( Locale.US ) ); // Or Locale.CANADA_FRENCH and so on.
SimpleDateFormat databaseDateTimeFormate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");SimpleDateFormat databaseDateFormate = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdf1 = new SimpleDateFormat("dd.MM.yy");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, MMM d, ''yy");SimpleDateFormat sdf4 = new SimpleDateFormat("h:mm a");SimpleDateFormat sdf5 = new SimpleDateFormat("h:mm");SimpleDateFormat sdf6 = new SimpleDateFormat("H:mm:ss:SSS");SimpleDateFormat sdf7 = new SimpleDateFormat("K:mm a,z");SimpleDateFormat sdf8 = new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa");
String currentDateandTime = databaseDateTimeFormate.format(new Date()); //2009-06-30 08:29:36String currentDateandTime = databaseDateFormate.format(new Date()); //2009-06-30String currentDateandTime = sdf1.format(new Date()); //30.06.09String currentDateandTime = sdf2.format(new Date()); //2009.06.30 AD at 08:29:36 PDTString currentDateandTime = sdf3.format(new Date()); //Tue, Jun 30, '09String currentDateandTime = sdf4.format(new Date()); //8:29 PMString currentDateandTime = sdf5.format(new Date()); //8:29String currentDateandTime = sdf6.format(new Date()); //8:28:36:249String currentDateandTime = sdf7.format(new Date()); //8:29 AM,PDTString currentDateandTime = sdf8.format(new Date()); //2009.June.30 AD 08:29 AM
日期格式
G Era designator (before christ, after christ)y Year (e.g. 12 or 2012). Use either yy or yyyy.M Month in year. Number of M's determine length of format (e.g. MM, MMM or MMMMM)d Day in month. Number of d's determine length of format (e.g. d or dd)h Hour of day, 1-12 (AM / PM) (normally hh)H Hour of day, 0-23 (normally HH)m Minute in hour, 0-59 (normally mm)s Second in minute, 0-59 (normally ss)S Millisecond in second, 0-999 (normally SSS)E Day in week (e.g Monday, Tuesday etc.)D Day in year (1-366)F Day of week in month (e.g. 1st Thursday of December)w Week in year (1-53)W Week in month (0-5)a AM / PM markerk Hour in day (1-24, unlike HH's 0-23)K Hour in day, AM / PM (0-11)z Time Zone
Date date = new Date();SimpleDateFormat dateFormatWithZone = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.getDefault());String currentDate = dateFormatWithZone.format(date);
DateFormat df = new SimpleDateFormat("HH:mm"); // Format timeString time = df.format(Calendar.getInstance().getTime());
DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd"); // Format dateString date = df1.format(Calendar.getInstance().getTime());
Time t = new Time(Time.getCurrentTimezone());t.setToNow();String date1 = t.format("%Y/%m/%d");
Date date = new Date(System.currentTimeMillis());SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);String var = dateFormat.format(date);String horafecha = var+ " - " + date1;
tvTime.setText(horafecha);
public static void getCurrentTimeUsingDate() {Date date = new Date();String strDateFormat = "hh:mm:ss a";DateFormat dateFormat = new SimpleDateFormat(strDateFormat);String formattedDate= dateFormat.format(date);Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();}
使用日历的时间
public static void getCurrentTimeUsingCalendar() {Calendar cal = Calendar.getInstance();Date date=cal.getTime();DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");String formattedDate=dateFormat.format(date);Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();}
当地时间和日期
public static void getCurrentTime(){System.out.println("-----Current time of your time zone-----");LocalTime time = LocalTime.now();Toast.makeText(this, time, Toast.LENGTH_SHORT).show();}
区域明智的时间
public static void getCurrentTimeWithTimeZone(){Toast.makeText(this, "Current time of a different time zone using LocalTime", Toast.LENGTH_SHORT).show();
ZoneId zoneId = ZoneId.of("America/Los_Angeles");LocalTime localTime=LocalTime.now(zoneId);DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");String formattedTime=localTime.format(formatter);Toast.makeText(this,formattedTime , Toast.LENGTH_SHORT).show();}
获取当前时间和日期的简单方法
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();
fun main(args: Array<String>) {println(System.currentTimeMillis()) // Current milliseconds
val date = Calendar.getInstance().time // Current date objectval date1 = Date(System.currentTimeMillis())
println(date.toString())println(date1.toString())
val now = Time(System.currentTimeMillis()) // Current time objectprintln(now.toString())
val sdf = SimpleDateFormat("yyyy:MM:dd h:mm a", Locale.getDefault())println(sdf.format(Date())) // Format current date
println(DateFormat.getDateTimeInstance().format(System.currentTimeMillis())) // using getDateTimeInstance()
println(LocalDateTime.now().toString()) // Java 8
println(ZonedDateTime.now().toString()) // Java 8}
// You can pass time zone and Local to getInstance() as parameter
Calendar calendar = Calendar.getInstance();
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);int currentMinute = 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);
public String getCurrentDate() {SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd hh:mm a zzz");Date date = new Date();sdf.setTimeZone(TimeZone.getTimeZone("GMT+6:00"));return sdf.format(date);}
val date=System.currentTimeMillis() //here the date comes in 13 digitsval dtlong = Date(date)val sdfdate = SimpleDateFormat(pattern, Locale.getDefault()).format(dtlong)
日期格式
"dd / MMMM / yyyy - HH:mm" -> 29 / April / 2022 - 12:03"dd / MM / yyyy" -> 29 / 03 / 2022"dd / MMM / yyyy" -> 29 / Mar / 2022 (shortens the month)"EEE, d MMM yyyy HH:mm:ss" -> Wed, 4 Jul 2022 12:08:56