获取本地日期与时间

public String  getCalendar() {
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date dt = new Date();
    return sdf.format(dt);
}

//SimpleDateFormat 时间格式

//y 年 MM 月 dd 日 

//D 一年的第几天 W 一个月的第几星期 w 一年的第几星期 k时 z时区

//HH 小时(24小时制) hh小时(12小时制) mm 分 ss 秒 SS 毫秒 E 星期 

//a 上午/下午

计算相隔天数

/**
 * 获得天数差
 * @param begin
 * @param end
 * @return
 */
public long getDayDiff(Date begin, Date end){
    long day = 1;
    if(end.getTime() < begin.getTime()){
        day = -1;
    }else if(end.getTime() == begin.getTime()){
        day = 1;
    }else {
        day += (end.getTime() - begin.getTime())/(24 * 60 * 60 * 1000) ;
    }
    return day-1;
}

Date date=new Date(2017-1900,11,1,0,30);

Date date2=new Date(2018-1900,11,1,0,30);

getDayDiff(date,date2)

计算时间差

   public static String getTimeDiffer(String date,String date2,String str){
        long diff,days,hours,minutes;
        @SuppressLint("SimpleDateFormat")
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try
        {
            Date d1 = df.parse(date);
            Date d2 = df.parse(date2);
            if(d1.getTime()>d2.getTime()) {
                diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
                days = diff / (1000 * 60 * 60 * 24);

                hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
                minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            }else{
                return "已到期";
            }
            if(days==0){
                if(hours==0){
                    return minutes+"分钟";
                }
            }
            if(str.equals("精确")){
                return ""+days+"天"+hours+"小时"+minutes+"分钟";
            }
            return ""+days+"天"+hours+"小时";
        }catch (Exception ignored){
        }
        return "";
    }

判断日期是否在指定日期

  public static int getTimeCompareSize2(String startTime, String endTime){
        int i=0;
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            if (date2.getTime()<date1.getTime()){
                i= 1;// 结束时间小于开始时间
            }else if (date2.getTime()==date1.getTime()){
                i= 2;//开始时间与结束时间相同
            }else if (date2.getTime()>date1.getTime()){
                i= 3;//结束时间大于开始时间
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  i;
    }

获取网络时间与日期

private void getNetTime() {
     URL url = null;
     try {
          url = new URL("http://www.baidu.com");
          URLConnection uc = url.openConnection();
          uc.connect();
          long ld = uc.getDate(); //取得网站日期时间
          @SuppressLint("SimpleDateFormat")
          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(ld);
          final String format = formatter.format(calendar.getTime());
          boottom_tiem.setText(format);
     } catch (Exception e) {
         e.printStackTrace();
     }
}

Android 时间与日期操作类的更多相关文章

  1. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  2. 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间

    public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...

  3. JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树

    /** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...

  4. 菜鸡的Java笔记 日期操作类

    日期操作类        Date 类与 long 数据类型的转换        SimpleDateFormat 类的使用        Calendar 类的使用                如 ...

  5. Lua库之时间和日期操作

    Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...

  6. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...

  7. 日期操作类--GregorianCalendar类

    GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...

  8. 日期操作类--SimpleDateFormat类

    使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...

  9. 日期操作类--DateFormat类

    简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...

随机推荐

  1. Java实现大数相加、相乘(不使用BigInteger)

    大数相加: package algorithm; //使用BigInteger类验证 import java.math.BigInteger; public class BigAdd { public ...

  2. Python3基础语法你学会了么

      编码 默认:源码文件以UTF-8编码,字符串都是unicode字符串 指定:   标识符 第一个字符:字母表中的字符或下划线 _ 其它部分:由字母.数字.下划线 _ 组成 大小写敏感 python ...

  3. Linux - 利用systemctl命令管理服务

    systemctl命令是系统服务管理器指令,融合了service和chkconfig的功能,可以查看和设置服务. 这里以docker服务为例. 利用systemctl命令管理 显示服务状态:syste ...

  4. postgresql-无序uuid tps测试

    # postgresql-无序uuid tps测试 ## 无序uuid对数据库的影响 由于最近在做超大表的性能测试,在该过程中发现了无序uuid做主键对表插入性能有一定影响.结合实际情况发现当表的数据 ...

  5. 深入理解css3中 nth-child 和 nth-of-type 的区别

    在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...

  6. requestAnimFrame 动画的使用方法

    //requestAnimFrame 封装,可以兼容所有浏览器 window.requestAnimFrame = (function(){ return window.requestAnimatio ...

  7. CSS 将一个页面平均分成四个部分(div)

    在项目中遇到需求,数据监控页面需要同时显示4个板块内容,如下图: CSS 如何将一个页面平均分成四个部分(div)呢? <!DOCTYPE html> <html lang=&quo ...

  8. 【转】ASP.NET Core MVC 配置全局路由前缀

    本文地址:http://www.cnblogs.com/savorboard/p/dontnet-IApplicationModelConvention.html作者博客:Savorboard 前言 ...

  9. Android_TextView使用Spanable

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  10. 从零开始学 Web 之 HTML5(四)拖拽接口,Web存储,自定义播放器

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...