android simpledateformat 星期 Android SimapleDateFormat 日期格式的转换方法

Android获得当前系统时间有好几种方法,本文使用的是System.currentTimeMillis->Date+SimpleDateFormat,我觉得这种方式来展示时间非常的灵活。

执行结果图:

package com.example.shen.systemtime;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        long time=System.currentTimeMillis();
        Date date=new Date(time);
        SimpleDateFormat format=new SimpleDateFormat(yyyy年MM月dd日 HH时mm分ss秒 EEEE);
        Log.e(time,time1=+format.format(date));
        format=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
        Log.e(time,time2=+format.format(date));
        format=new SimpleDateFormat(yyyy/MM/dd);
        Log.e(time,time3=+format.format(date));
        format=new SimpleDateFormat(HH:mm:ss);
        Log.e(time,time4=+format.format(date));
        format=new SimpleDateFormat(EEEE);
        Log.e(time,time5=+format.format(date));
        format=new SimpleDateFormat(E);
        Log.e(time,time6=+format.format(date));
    }
}


Android SimapleDateFormat 日期格式的转换方法

</pre><pre code_snippet_id="1936530" snippet_file_name="blog_20161019_2_7104315" name="code" class="java"><pre name="code" class="java">/** 
	  * @param nowDateFormat 现在的字符串格式  yyyy年MM月dd日 
	  * StringUtils.getDateFormat("yy年MM月dd日 HH:mm EEEE", "yyyy年MM月dd日", msgInbox.getDate())
	  * @param toDateFormat 你最终要的格式 
	  * @param sourDate 你的日期字符串;
	  * @return 
	  */  
	public static String getDateFormat(String nowDateFormat,String toDateFormat,String sourDate) {  
	  // 分析从 ParsePosition 给定的索引处开始的文本。如果分析成功,则将 ParsePosition 的索引更新为所用最后一个字符后面的索引  
		SimpleDateFormat nowFormat = new SimpleDateFormat(nowDateFormat);  
		SimpleDateFormat toFormat = new SimpleDateFormat(toDateFormat);  
	  ParsePosition position = new ParsePosition(0);
	  Date dateValue = nowFormat.parse(sourDate, position); 
	  
	  String returnString = toFormat.format(dateValue);  
	  return returnString;  
	 }

 

例:sourDateFormat = “yyyy-MM” sourDate = "2012-11-15"转换结果为:"2012-11"
 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值