Android 时间格式处理

本文主要探讨了在Android中如何进行时间格式的处理,参考了多个来源,包括lszdb1983的博客、OSChina上的代码片段以及CSDN上lenmoyouzi的文章。内容涉及日期和时间的解析、格式化以及在Android应用中的实际运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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

import android.R.integer;
import android.content.Context;
import android.text.format.DateUtils;
import android.text.format.Time;

public class TimeOperater {

	private Context mContext;
	
	private static int flagsDate = DateUtils.FORMAT_SHOW_DATE;//5月11日
	private static int flagsTime = DateUtils.FORMAT_SHOW_TIME ;//16:40(是二进制显示 下午 04:40)
	private static int flagsWeek = DateUtils.FORMAT_SHOW_WEEKDAY;//星期2
	private static int flagsAMPM = DateUtils.FORMAT_CAP_AMPM;
	private static int flagsTimeStyle = DateUtils.FORMAT_12HOUR;//十二小时制
	
	public TimeOperater(){
		
	}
	public TimeOperater(Context context){
		
		this.mContext = context;
	}
	/**
	 * 返回自1970年1月1日  至今的毫秒数
	 * 
	 * */
	public long getTime(){
		
		long currentTime = System.currentTimeMillis();
		
		return currentTime;
	}
	/**
	 * 时间格式转换
	 * String ->Date
	 * 
	 * */
	public  Date strToDate(String str){
		
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	     Date date = null;
	     try {
	    	 
	    	 date = format.parse(str);
	      
	     } catch (ParseException e) {
	    	 
	    	 e.printStackTrace();
	     }
	     return date;
	
	}
	/**
	 * 时间格式转换
	 * Date ->毫秒
	 * 
	 * */
	public  long dateToMillis(Date date){
		
		long mills = date.getTime();
		
		return mills;
	}

	/**
	 * 显示时间格式为今天、昨天、yyyy/MM/dd hh:mm
	 * 
	 * @param context
	 * @param when
	 * @return String
	 */
	public  String formatTimeString(long when) {
		
		Time then = new Time();
		then.set(when);
		Time now = new Time();
		now.setToNow();

		if(then.year == now.year) {
			//今年
			if (then.yearDay == now.yearDay) {
				//今天  --显示格式 上午 10:30
				String time = (String)DateUtils.formatDateTime(mContext, when, flagsTime|flagsTimeStyle);//12小时制
				return time;
				
			} else if ((now.yearDay - then.yearDay) == 1) {
				//昨天 --显示格式 昨天 10:30
				String time = (String)DateUtils.formatDateTime(mContext, when, flagsTime);//24小时制
				return "昨天" + time;
				
			}else if ((now.yearDay - then.yearDay) == 2) {
				//前天 -- 显示格式  前天 11:30
				String time = (String)DateUtils.formatDateTime(mContext, when, flagsTime);//24小时制
				return "前天"+ time;
				
			}else {
				//今年更早之前 MM月dd日
				String flag = "yyyy年MM月dd日";
		
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(flag);
				String time = simpleDateFormat.format(when);
				
				return time;
			}
		}else {
			//不是今年 -- 显示格式 yyyy年MM月dd日

			String flag = "MM月dd日";
			
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(flag);
			String time = simpleDateFormat.format(when);
			
			return time;
		}
			
		
	}
	
	
	
	
	
	
	
	
}


参考:

http://lszdb1983.blog.163.com/blog/static/204263482012010614570/


http://www.oschina.net/code/snippet_575610_22694


http://blog.csdn.net/lenmoyouzi/article/details/17524779



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值