关闭

代码小工具

97人阅读 评论(0) 收藏 举报

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 " ";
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2152次
    • 积分:73
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档