TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
方式一:
- import java.text.SimpleDateFormat;
- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
- Date curDate = new Date(System.currentTimeMillis());
- //获取当前时间
复制代码- String str = formatter.format(curDate);
复制代码
方式二:
- 取得系统时间
- 1。
- long time=System.currentTimeMillis();
-
- 2。
- final Calendar mCalendar=Calendar.getInstance();
- mCalendar.setTimeInMillis(time);
- 取得小时:mHour=mCalendar.get(Calendar.HOUR);
- 取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
-
-
- 3。
- Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
- t.setToNow(); // 取得系统时间。
- int year = t.year;
- int month = t.month;
- int date = t.monthDay;
- int hour = t.hour; // 0-23
-
- 4。
- DateFormat df = new SimpleDateFormat("HH:mm:ss");
- df.format(new Date());
复制代码- package com.example.time;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.TimeZone;
- import android.app.Activity;
- import android.os.Bundle;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- new Thread(){
- public void run() {
- String time = Local2UTC("2018-03-19 19:41:44");
- System.out.println("time"+time);
- };
- }.start();
- }
- /**
- * 将本地时间转为UTC时间
- * @param strDate:需要转化的date
- * @return
- */
- public String Local2UTC(String strDate){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String gmtTime = "";
- sdf.setTimeZone(TimeZone.getTimeZone("gmt"));
- gmtTime = sdf.format(stringToDate(strDate, "yyyy-MM-dd HH:mm:ss"));
- return gmtTime;
- }
- /**
- * 将string类型转换为date类型
- * @param strTime:要转换的string类型的时间,时间格式必须要与formatType的时间格式相同
- * @param formatType:要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
- * @return
- */
- private Date stringToDate(String strTime, String formatType){
- SimpleDateFormat formatter = new SimpleDateFormat(formatType);
- Date date = null;
- try {
- date = (Date) formatter.parse(strTime);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- }
复制代码
|
|