当前位置: 移动技术网 > IT编程>移动开发>Android > Android 获取时间实例代码

Android 获取时间实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

薄 来,溴化钠口服液,毛衣编织大全

 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;
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网