代码小工具

1.日期类型的转换:

package net.uct.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeChange {
	// 2011-11-15ת��ΪDate(1321347780000+0800)/
	public static String StringChanged(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat timeZoneSdf = new SimpleDateFormat("Z");
		try {
			Date date = sdf.parse(time);
			String timeZone = timeZoneSdf.format(date);
			String timex = "/Date(" + date.getTime() + timeZone + ")/";
			return timex;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	// Date(1321347780000+0800)/ת��Ϊ2011-11-15
	public static String LongChanged(String time) {
		if(!time.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String regEx = "/Date\\(([0-9]+)([\\+\\-]{1}[0-9]+)\\)/";
			Pattern pattern = Pattern.compile(regEx);
			Matcher matcher = pattern.matcher(time);
			if (matcher.find()) {
				String time1 = matcher.group(1);
				String timeZone = matcher.group(2);
				Date date = new Date(new Long(time1));
				TimeZone tz = TimeZone.getTimeZone("GMT" + timeZone);  
				sdf.setTimeZone(tz);
				String time2 = sdf.format(date);  
				return time2;
			}
		}

		return " ";
	}
}


阅读更多
上一篇一步一步的学习android应用开发到系统底层开发之android开发层次及所需技术
下一篇学习笔记:const int *pi 与 int *const pi 区别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭