您的位置:首页 > 职场人生

《黑马程序员》 日期Data类,日期格式化DateFormat,自定义格式化SimpleDateFormat的使用

2014-03-06 13:19 477 查看
------- android培训java培训、期待与您交流! ----------
public static void main(String[] args) {
// getOsDateInstance();
// testDate();
// testDateTime();
// testDateTime2();
// testSimpleDateFormat();
testSimpleDateFormat2();
}

private static void testSimpleDateFormat2() {
// 把字符串值的日期给转换成日期对象
String oldStr = "2008-10-19 10:11:30.345";
String strFt = "yyyy-mm-dd hh:mm:ss.SSS";
DateFormat dateFormat = new SimpleDateFormat(strFt);
try {
Date newDate = dateFormat.parse(oldStr);
System.out.println(newDate);
// Sat Jan 19 10:11:30 CST 2008
} catch (ParseException e) {
e.printStackTrace();
}
}

/**
* 格式化日期:一
*/
private static void testSimpleDateFormat() {
// simapledateformat通常用于将日期从一种格式转换成另外一种日期格式
// 如原日期为2008-10-19 10:11:30.345,转换后日期为2008年10月19日10时11分30秒345毫秒
String oldStr = "2008-10-19 10:11:30.345";
// 转换之前的格式
String temp1 = "yyyy-mm-dd hh:mm:ss.SSS";
// 转换之后的格式
String temp2 = "yyyy年mm月dd日 hh时mm分ss秒SSS毫秒";
DateFormat smp1 = new SimpleDateFormat(temp1);
DateFormat smp2 = new SimpleDateFormat(temp2);
Date d = null;
try {
// 把字符串的日期转换成一个新的日期对象
d = smp1.parse(oldStr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(smp2.format(d));
// 2008年11月19日 10时11分30秒345毫秒
}

private static void testDateTime2() {
DateFormat df1;
DateFormat df2;
df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD, new Locale(
"zh", "CN"));
System.out.println("日期的格式化:" + df1.format(new Date()));
df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,
DateFormat.ERA_FIELD, new Locale("zh", "CN"));
System.out.println("日期和时间的格式化:" + df2.format(new Date()));
// 日期的格式化:2014年3月5日
// 日期和时间的格式化:2014年3月5日 上午11时39分29秒 CST
}

private static void testDateTime() {
Date date = new Date();
DateFormat df = DateFormat.getDateTimeInstance(); // 这里获取到默认的日期格式化的实例
String dateStr1 = df.format(date);
System.out.println("默认的datetime格式化:" + dateStr1);
DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
String dateStr3 = df3.format(date);
System.out.println("使用FULL风格格式化datetime:" + dateStr3);
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
String dateStr4 = df4.format(date);
System.out.println("使用SHORT风格格式化datetime:" + dateStr4);
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String dateStr5 = df5.format(date);
System.out.println("使用LONG风格格式化datetime:" + dateStr5);
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
String dateStr6 = df6.format(date);
System.out.println("使用MEDIUM风格格式化datetime:" + dateStr6);
// 使用时区:Locale.CHINESE 和Locale.CHINA都是一样
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM, Locale.CHINESE);
String dateStr7 = df7.format(date);
System.out.println("使用MEDIUM风格及中国地区格式化datetime:" + dateStr7);
/*
* 默认的datetime格式化:2014-3-5 11:13:18 使用FULL风格格式化datetime:2014年3月5日 星期三
* 上午11时13分18秒 CST 使用SHORT风格格式化datetime:14-3-5 上午11:13
* 使用LONG风格格式化datetime:2014年3月5日 上午11时13分18秒
* 使用MEDIUM风格格式化datetime:2014-3-5 11:13:18
* 使用MEDIUM风格及中国地区格式化datetime:2014-3-5 11:14:41
*/
}

private static void testDate() {
Date date = new Date();
DateFormat df = DateFormat.getInstance(); // 这里获取到默认的日期格式化的实例
// Get a default date/time formatter that uses the SHORT style for both
// the date and the time.
// 默认使用的是短样式进行格式化日期和时间
String dateStr1 = df.format(date);
System.out.println("默认的格式化:" + dateStr1);
// 默认的格式化:14-3-5 上午10:56
// 我们使用
DateFormat df2 = DateFormat.getDateInstance(); // 我们获取日期格式化实例
// Gets the date formatter with the default formatting style for the
// default locale.
// 这个是使用本地的默认格式化格式进行格式化这个取决于操作系统的版本和区域
// 这个是根据locale得到的默认的格式化对象
String dateStr2 = df2.format(date);
System.out.println("locale默认格式化:" + dateStr2);
// locale默认格式化:2014-3-5 这种使用的也是短的风格
DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL);
String dateStr3 = df3.format(date);
System.out.println("使用FULL风格格式化:" + dateStr3);
// 使用full风格格式化:2014年3月5日 星期三
DateFormat df4 = DateFormat.getDateInstance(DateFormat.SHORT);
// 使用SHORT风格格式化:14-3-5
String dateStr4 = df4.format(date);
System.out.println("使用SHORT风格格式化:" + dateStr4);
DateFormat df5 = DateFormat.getDateInstance(DateFormat.LONG);
// 使用LONG风格格式化:2014年3月5日
String dateStr5 = df5.format(date);
System.out.println("使用LONG风格格式化:" + dateStr5);
DateFormat df6 = DateFormat.getDateInstance(DateFormat.MEDIUM);
String dateStr6 = df6.format(date);
System.out.println("使用MEDIUM风格格式化:" + dateStr6);
// 使用MEDIUM风格格式化:2014-3-5

DateFormat df7 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA);
String dateStr7 = df7.format(date);
System.out.println("使用MEDIUM风格及中国地区格式化:" + dateStr7);
// 使用MEDIUM风格及中国地区格式化:2014-3-5

// 使用getDateInstance只能对日期进行格式化,对时间并不能格式化
// 我们下面只演示日期和时间的。单纯的时间就不测试了
}

private static void getOsDateInstance() {
Date date = new Date();
System.out.println("系统当前日期:" + date);
// 系统当前日期:Wed Mar 05 10:54:50 CST 2014
// 不符合我们看到的日期格式也没有精确到毫秒
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息