初涉江湖,还望海涵!
写点东西,纯粹是由于我的的记忆能力较弱,写些笔记罢了,如有错误还望雅正!java
代码粘贴api
public class MainActivity extends AppCompatActivity { public static final String TAG = "MainActivity"; @RequiresApi(api = Build.VERSION_CODES.CUPCAKE) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //timestamp TextView timestamp = findViewById(R.id.timestamp_show); timestamp.setText("timestamp:" + System.currentTimeMillis()); //date Date date = new Date(); TextView date_show = findViewById(R.id.date_show); date_show.setText("Date:" + date.toString()); //Calendar TextView calendar_show = findViewById(R.id.calendar_show); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); String calendar_show_string = "Calendar:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; calendar_show.setText(calendar_show_string); //Time TextView time_show = findViewById(R.id.time_show); Time time = new Time(); time.setToNow(); int time_year = time.year; int time_month = time.month; int time_day = time.monthDay; int time_hour = time.hour; int time_minute = time.minute; int time_second = time.second; String time_show_string = "Time:" + time_year + "-" + time_month + "-" + time_day + " " + time_hour + ":" + time_minute + ":" + time_second; time_show.setText(time_show_string); //SimpleDateFormat TextView simpleDateFormat_show = findViewById(R.id.simpleDateFormat_show); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String simpleDateFormat_tring = "SimpleDateFormat:" + format.format(new Date()); simpleDateFormat_show.setText(simpleDateFormat_tring); Log.d(TAG, "onCreate: Long的最大值:" + Long.MAX_VALUE); }
1 timestamp
2 date
3 SimpleDateFormat
4 Calendar
5 Time
//timestamp TextView timestamp = findViewById(R.id.timestamp_show); timestamp.setText("timestamp:" + System.currentTimeMillis());
timestamp,时间戳。
使用时调用System类的currentTimeMillis()方法,该方法的描述是:数组
/** * Returns the current time in milliseconds. Note that * while the unit of time of the return value is a millisecond, * the granularity of the value depends on the underlying * operating system and may be larger. For example, many * operating systems measure time in units of tens of * milliseconds. * * <p> See the description of the class <code>Date</code> for * a discussion of slight discrepancies that may arise between * "computer time" and coordinated universal time (UTC). * * @return the difference, measured in milliseconds, between * the current time and midnight, January 1, 1970 UTC. * @see java.util.Date */ public static native long currentTimeMillis();
能够看出,该方法返回的是long类型的结果,结果记录的是midnight, January 1, 1970 UTC至今通过的毫秒数(milliseconds)。安全
System.currentTimeMillis()是一个native方法,是一个C/C++方法,由系统测量时间戳并返回测量结果,根据注释描述,测量结果可能偏大,由于有些操做系统测量时间是以十毫秒为单位的,类Date中讨论了关于系统时间和UTC时间产生差别的缘由,可自行观看!多线程
Note:ide
//date Date date = new Date(); TextView date_show = findViewById(R.id.date_show); date_show.setText("Date:" + date.toString());
经过实例化Date类获取date实例从而获取时间,简单经过toString()打印结果函数
Date类的注释特别描述了ui
构造函数this
public Date() { this(System.currentTimeMillis()); } public Date(long date) { fastTime = date; } /** * @param year the year minus 1900. * @param month the month between 0-11. * @param date the day of the month between 1-31. * @param hrs the hours between 0-23. * @param min the minutes between 0-59. * @param sec the seconds between 0-59. * @see java.util.Calendar * @deprecated As of JDK version 1.1, * replaced by <code>Calendar.set(year + 1900, month, date, * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900, * month, date, hrs, min, sec)</code>. */ @Deprecated public Date(int year, int month, int date, int hrs, int min, int sec) { int y = year + 1900; // month is 0-based. So we have to normalize month to support Long.MAX_VALUE. if (month >= 12) { y += month / 12; month %= 12; } else if (month < 0) { y += CalendarUtils.floorDivide(month, 12); month = CalendarUtils.mod(month, 12); } BaseCalendar cal = getCalendarSystem(y); cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef()); cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0); getTimeImpl(); cdate = null; }
无参大Date()直接把System.currentTimeMillis()的时间戳返回给fastTime,另外一个就是设定好年月日时分秒来建立对象,其中的设定是年是1900+参数year而且也对月份超出范围作出了处理,可是该构造方法已经是@Deprecated(弃用)了spa
Date类中大部分的方法都已经弃用,要特别是单独获取年或者月等信息的方法,基本上都已经弃用,留下的有打印即toString()和一些比较等功能性的方法
//SimpleDateFormat TextView simpleDateFormat_show = findViewById(R.id.simpleDateFormat_show); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String simpleDateFormat_tring = "SimpleDateFormat:" + format.format(new Date()); simpleDateFormat_show.setText(simpleDateFormat_tring);
SimpleDateFormat类的核心是Text的formatting(格式化)和Time的parsing(解析),SimpleDateFormat()经过传入一个字符串来格式化须要的表现形式,样例中经过调用format()传入Date无参对象,其实是调用System.currentTimeMillis()获取最基本的时间,SimpleDateFormat类的做用是把传入的Date类时间定制化封装,从而获得须要的结果。
Note:
关于SimpleDateFormat类,能够很自由的定制表现形式,年月日时分秒,时间格式,AD/BC。。。
定制化所用字母的含义:
定制化使用"字符串",在该字符串中使用'字符'表示在年月日等数据外的部分,如分隔符
SimpleDateFormat类的时间格式定制包括年月日等数据的表现形式,链接符,日期格式的描述,如Time zone,AM/PM,AD/BC。。。
SimpleDateFormat类中存在的问题是线程同步
/** * Date formats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * externally. */
SimpleDateFormat是线程不一样步的,要在多线程中使用则要在线程外同步.
//Calendar TextView calendar_show = findViewById(R.id.calendar_show); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); String calendar_show_string = "Calendar:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; calendar_show.setText(calendar_show_string);
Calendar是一个抽象类经过其内定义的Calendar.getInstance()静态方法实例化对象而该静态方法最终是经过返回一个new GregorianCalendar(zone, aLocale)来实现初始化!
Calendar类内部定义了关于时间须要用到的索引并用一个int数组存储相关数据
public final static int ERA = 0; public final static int YEAR = 1; public final static int MONTH = 2; public final static int WEEK_OF_YEAR = 3; ... @SuppressWarnings("ProtectedField") protected int fields[]; public int get(int field) { complete(); return internalGet(field); } protected final int internalGet(int field) { return fields[field]; }
Calendar类的简单实用就是经过调用get方法从数组中获取相应的数据
//Time TextView time_show = findViewById(R.id.time_show); Time time = new Time(); time.setToNow(); int time_year = time.year; int time_month = time.month; int time_day = time.monthDay; int time_hour = time.hour; int time_minute = time.minute; int time_second = time.second; String time_show_string = "Time:" + time_year + "-" + time_month + "-" + time_day + " " + time_hour + ":" + time_minute + ":" + time_second; time_show.setText(time_show_string);
把这段代码打入到剪辑器,你会看到Time这个类是弃用了的
官方的注释解释是这样的
/** * An alternative to the {@link java.util.Calendar} and * {@link java.util.GregorianCalendar} classes. An instance of the Time class represents * a moment in time, specified with second precision. It is modelled after * struct tm. This class is not thread-safe and does not consider leap seconds. */
能够看到,描述上说,这是线程不安全的类,同时也没有处理leap seconds(闰秒)的能力,还举出了几个例子。
虽然是弃用的方法,可是仍是能够看看怎么使用Time类的,简单地说,就是经过对象.变量的形式获取,也就是说,Time不像Calendar类那样使用数组存储数据,Time就是经过建立public int 数据 的形式来保存数据,也就是这些数据都是public的
总的来讲,获取数据的时候,经过Time的形式,如int time_hour = time.hour;这样的写法,其实才是最舒服的(我的感受),固然,最重要的仍是安全问题