大家好:
今天遇到的一个问题是,格式转换问题,
dayTime = DateUtils.convertDate2String("yyyy-MM-dd EE",DateUtils.parseDate(dayTime));
这是DateUtils类里的方法(公共类方法)
/**
* 转换日期得到指定格式的日期字符串
*
* @param formatString
* 需要把目标日期格式化什么样子的格式。例如,yyyy-MM-dd HH:mm:ss
* @param targetDate
* 目标日期
* @return
*/
public static String convertDate2String(String formatString, Date targetDate) {
SimpleDateFormat format = null;
String result = null;
if (targetDate != null) {
format = new SimpleDateFormat(formatString);
result = format.format(targetDate);
} else {
return null;
}
return result;
}
按理说 的确实现了转换 年 月 日 星期
但是,在linux系统中,星期几却成了英文的!
这个问题纠结着我,有点郁闷啊,所以我又换了个方法,
/**
* 根据当前给定的日期获取当前天是星期几(中国版的)
*
* @param date
* 任意时间
* @return
*/
public static String getChineseWeek(Calendar date) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
return dayNames[dayOfWeek - 1];
}
这里用到了Calendar 我查询资料,得知
从JDK1.1版本开始,在处理日期和时间时,系统推荐使用Calendar类进行实现
Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建对象的过程对程序员来说是透明的,只需要使用getInstance方法创建即可。
详情见:http://blog.csdn.net/kyfg27_niujin/article/details/8077828
查询后发现转换格式可以有以下方法,供大家参考,我也顺便记载一下!
1.Calendar 转化 String
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
2.String 转化Calendar
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
3.Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
4.String 转化Date
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);
5.Date 转化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
6.Calendar转化Date
Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();
7.String 转成 Timestamp
Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
8.Date 转 TimeStamp
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
我用了date转calendar的方法,实现我的功能
例子仅供参考:
String dayTimetest;
Date date;
dayTime = DateUtils.convertDate2String("yyyy-MM-dd",new Date()); //当前系统时间的 年 月 日 附给daytime
try {
date = DateUtils.convertString2Date(dayTime); //将String转换成Date
Calendar calendar = Calendar.getInstance(); //date转换成 Calendar
calendar.setTime(date);
dayTimetest = DateUtils.getChineseWeek(calendar);
dayTime = dayTime+" "+dayTimetest;
} catch (ParseException e) {
e.printStackTrace();
}
DateUtils类中的两个方法:
/**
* 转换字符串得到默认格式的日期类型
*
* @param strDate
* @return
* @throws ParseException
*/
public static Date convertString2Date(String strDate) throws ParseException {
Date result = null;
try {
result = convertString2Date(DEFAILT_DATE_PATTERN, strDate);
} catch (ParseException pe) {
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return result;
}
/**
* 根据当前给定的日期获取当前天是星期几(中国版的)
*
* @param date
* 任意时间
* @return
*/
public static String getChineseWeek(Calendar date) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
return dayNames[dayOfWeek - 1];
}