重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android应用中怎么对时间进行获取

今天就跟大家聊聊有关Android应用中怎么对时间进行获取,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联建站-专业网站定制、快速模板网站建设、高性价比会宁网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式会宁网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖会宁地区。费用合理售后完善,十多年实体公司更值得信赖。

 Android 获取时间实例代码

注意:

h:12小时制小时数
H:24小时制小时数

实例代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/8.
 */
public class GetTime {

  public static void main(String[] args) {
    Date date = new Date();
    System.out.println(date);//Mon May 08 14:27:44 CST 2017
    System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));//2017-05-08 02:27:44

    long millis = System.currentTimeMillis();
    System.out.println(millis);//1494224864479
    System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(millis));//2017-05-08 02:27:44

    //yyyy-MM-dd  E  hh:mm:ss.sss
    //年-月-日   星期  时:分:秒.毫秒
    System.out.println(new SimpleDateFormat("yyyy-MM-dd E hh:mm:ss.sss").format(date));//2017-05-08 星期一 02:27:44.044
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(date));//2017-05-08 14:27:44.044
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));//2017-05-08 14:27:44
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));//2017-05-08 14:27
    System.out.println(new SimpleDateFormat().format(date));//17-5-8 下午2:27 :默认

    compareDataToNow("2017-05-03 12:45:00");

    try {
      Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-05-03 12:45:00");
      compareToNowDate(date1);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    getWeek();
    getTime1();
    getTime2();
  }


  static void getTime1() {
    long time = System.currentTimeMillis();
    //long now = android.os.SystemClock.uptimeMillis();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d1 = new Date(time);
    String t1 = format.format(d1);
    System.out.println("SimpleDateFormat时间===" + t1);//2017-05-08 12:44:10

    SimpleDateFormat f4 = new SimpleDateFormat("今天是" + "yyyy年MM月dd日 E kk点mm分");
    System.out.println("f4======" + f4.format(new Date()));//今天是2017年05月08日 星期一 14点15分

    SimpleDateFormat f3 = new SimpleDateFormat("今天是" + "hh小时mm分钟");
    System.out.println("f3======" + f3.format(new Date()));//今天是02小时15分钟

    SimpleDateFormat f2 = new SimpleDateFormat("今天是" + "kk点mm分钟");
    System.out.println("f2======" + f2.format(new Date()));//今天是14点17分钟
  }

  static void getTime2() {
    Calendar calendar = Calendar.getInstance();
    String created = calendar.get(Calendar.YEAR) + "年"
        + (calendar.get(Calendar.MONTH) + 1) + "月"//从0计算
        + calendar.get(Calendar.DAY_OF_MONTH) + "日"
        + calendar.get(Calendar.HOUR_OF_DAY) + "时"
        + calendar.get(Calendar.MINUTE) + "分" + calendar.get(Calendar.SECOND) + "s";
    System.out.println("Calendar时间====" + created);//时间:2017年5月8日12时33分24s
  }

  static void getWeek() {
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    String today = null;
    if (day == 2) {
      today = "Monday";
    } else if (day == 3) {
      today = "Tuesday";
    } else if (day == 4) {
      today = "Wednesday";
    } else if (day == 5) {
      today = "Thursday";
    } else if (day == 6) {
      today = "Friday";
    } else if (day == 7) {
      today = "Saturday";
    } else if (day == 1) {
      today = "Sunday";
    }
    System.out.println("Today is:- " + today);//Today is:- Monday
  }

  //计算日期之间相隔几天:
  static long compareDataToNow(String date) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date passDate, nowDate;
    long diff = -100l, days = -100l;

    try {
      passDate = sdf.parse(date);

      String nowStr = sdf.format(new Date());
      nowDate = sdf.parse(nowStr);

      diff = passDate.getTime() - nowDate.getTime();//long型的毫秒数
      days = diff / (1000 * 60 * 60 * 24);
      System.out.println("相隔:" + days + "天" + " nowDate.getTime()=====" + nowDate.getTime());//-5天
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return diff;
  }

 //计算日期之间相隔几天:
  static long compareToNowDate(Date date) {
    long diff = -100l, days = -100l;

    Date nowDate = new Date();

    diff = date.getTime() - nowDate.getTime();//long型的毫秒数
    days = diff / (1000 * 60 * 60 * 24);
    System.out.println("相隔:" + days + "天" + " nowDate.getTime()=====" + nowDate.getTime());//-5天

    return diff;
  }
}

看完上述内容,你们对Android应用中怎么对时间进行获取有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前题目:Android应用中怎么对时间进行获取
URL地址:http://cqcxhl.com/article/jojsho.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP