android 时间获取以及时间格式化

Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现

现总结如下:

方法一;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void getTime1(){  
  2.         long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();  
  3.         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  4.         Date d1=new Date(time);  
  5.         String t1=format.format(d1);  
  6.         Log.e("msg", t1);  
  7.     }  

方法二;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");  
  2.         String t=format.format(new Date());  
  3.         Log.e("msg", t);  

方法三;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void getTime3(){  
  2.     Calendar calendar = Calendar.getInstance();  
  3.     String created = calendar.get(Calendar.YEAR) + "年"  
  4.             + (calendar.get(Calendar.MONTH)+1) + "月"//从0计算  
  5.             + calendar.get(Calendar.DAY_OF_MONTH) + "日"  
  6.             + calendar.get(Calendar.HOUR_OF_DAY) + "时"  
  7.             + calendar.get(Calendar.MINUTE) + "分"+calendar.get(Calendar.SECOND)+"s";  
  8.     Log.e("msg", created);  
  9.    }  
方法四;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void getTime4(){  
  2.         Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。  
  3.         t.setToNow(); // 取得系统时间。  
  4.         String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second;  
  5.         Log.e("msg", time);  
  6.     }  

获取星期日期:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Calendar calendar = Calendar.getInstance();  
  2.             int day = calendar.get(Calendar.DAY_OF_WEEK);  
  3.             String today = null;  
  4.             if (day == 2) {  
  5.                 today = "Monday";  
  6.             } else if (day == 3) {  
  7.                 today = "Tuesday";  
  8.             } else if (day == 4) {  
  9.                 today = "Wednesday";  
  10.             } else if (day == 5) {  
  11.                 today = "Thursday";  
  12.             } else if (day == 6) {  
  13.                 today = "Friday";  
  14.             } else if (day == 7) {  
  15.                 today = "Saturday";  
  16.             } else if (day == 1) {  
  17.                 today = "Sunday";  
  18.             }  
  19.             System.out.println("Today is:- " + today);  


最后说一下日期格式化,日期格式化通常使用SimpleDateFormat类实现,其中的日期格式不能够自己随意定义,主要有以下几种形式:

SimpleDateFormat f1= new SimpleDateFormat(); //其中没有些格式化参数,我们使用默认的日期格式。
System.out.println(f.formate(new Date()));
代码输出的日期格式为:12-3-22 下午4:36

SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");//可根据不同样式请求显示不同日期格式,要显示星期可以添加E参数
System.out.println(f4.format(new Date()));
代码输出的日期格式为:今天是2012年03月22日 星期四 16点46分

SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
 System.out.println("Date to String "+formater.format(new Date()));
相近的常用形式还有 yyMMdd hh:mm:ss  yyyy-MM-dd hh:mm:ss  dd-MM-yyyy hh:mm:ss


应有的时候通常还会需要把具体日期转换为毫秒或者Timestamp形式,如下:

文本 - > Timestamp,日期 -> Timestamp
  Timestamp t ;
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  try ...{
   t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
  } catch (ParseException e) ...{
   e.printStackTrace();
  }
 Timestamp t ;
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  t = new Timestamp(new Date().getTime());


日期比较,转换处理

[java]  view plain  copy
  1. public void compareToNowDate(Date date){  
  2.         Date nowdate=new Date();  
  3.         //format date pattern  
  4.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  5.         //convert to millions seconds  
  6.         long time=DateToLong(StringToDate(formatter.format(nowdate)));  
  7.         long serverTime=DateToLong(date);  
  8.         //convert to seconds  
  9.         long minTime=Math.abs(time-serverTime)/1000;    Log.d(getLocalClassName(), "minTime= "+minTime);  
  10.     }  
  11.       
  12.     private long DateToLong(Date time){  
  13.         SimpleDateFormat st=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyyMMddHHmmss  
  14.         return Long.parseLong(st.format(time));  
  15.     }  
  16.   
  17.     private Date StringToDate(String s){  
  18.         Date time=null;  
  19.         SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  20.         try {  
  21.             time=sd.parse(s);  
  22.         } catch (java.text.ParseException e) {  
  23.             System.out.println("输入的日期格式有误!");   
  24.             e.printStackTrace();  
  25.         }  
  26.         return time;  
  27.     }  

[java]  view plain  copy
  1. //计算日期之间相隔几天:  
  2.     public long compareDataToNow(String date){  
  3.            
  4.           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  5.           Date passDate,nowDate;  
  6.           long diff=-100l,days=-100l;  
  7.             
  8.          try {  
  9.              passDate = sdf.parse(date);  
  10.              String nowStr=sdf.format(new Date());  
  11.              nowDate=sdf.parse(nowStr);    
  12.                
  13.               diff = passDate.getTime() - nowDate.getTime();     
  14.                 days = diff / (1000 * 60 * 60 * 24);   
  15.                 System.out.println( "相隔:"+days+"天");  
  16.         } catch (ParseException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.          return diff;  
  20.     }  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值