文本字符串的处理,数字格式是第一常见的,日期格式就是第二常见的了。日期的格式转换,主要是四种:Date转String、String转Date、Date转Calendar、Calendar转Date。 Date转String,先设置要转换的日期格式,再做格式化,代码如下: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); //格式中间可以再插入/、-、:等日期时间分隔符 Date date = new Date(); String str = sdf.format(date); System.out.println("date="+date+", str="+str); String转Date SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String str = "20151124093336"; Date date = sdf.parse(str); System.out.println("date="+date+", str="+str); Date转Calendar Calendar calendar = Calendar.getInstance(); Date date = new Date(); calendar.setTime(date); System.out.println("date="+date+", calendar="+calendar); Calendar转Date Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); System.out.println("date="+date+", calendar="+calendar);
java的Date类型无法直接做加减操作,比如当前时间加一个月,或者减10天,Date类型是处理不了的,必须要借助于Calendar类型。先把Date类型转换为Calendar类型,然后对Calendar进行加减运算,最后再把Calendar类型转换为Date类型。代码如下:
import java.util.Calendar;
import java.util.Date;
public class AddDate {
public static Date addDate(Date date, int dayNum) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, dayNum);
return calendar.getTime();
}
public static Date addMonth(Date date, int monthNum) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, monthNum);
return calendar.getTime();
}
public static void main(String[] arg) {
Date old_date = new Date();
Date add_day = addDate(old_date, -10);
Date add_month = addMonth(old_date, 1);
System.out.println("old_date=" + old_date + ", add_day=" + add_day + ", add_month=" + add_month);
}
}
都说学以致用,博主初学android时,就是拿日历app来练手。日历的需求很简单,要有公历日期、农历日期,最好还有常见节日,当然若有二十四节气就更好了。 公历日期有规律,1、3、5、7、8、10、12这几个月有31天,4、6、9、11这几个月有30天,2月份平时28天,闰年有29天。闰年的判断规则也简单,能被4整除的年份是闰年,当然有例外能被100整除但不能被400整除的是平年,能被400整除的是闰年。另外判断某天是星期几,倒是有点麻烦,先要把它转成Calendar类型,然后根据Calendar.DAY_OF_WEEK取到一周中的第几天,但java中一周中的第一天是周日,按照我们的习惯第一天是周一,所以又得做个转换,代码如下:
public static int getWeekday(Date date){
int dayOfWeek = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
return dayOfWeek;
}
农历日期算起来有点麻烦,我就直接找了网上的开源代码。二十四节气其实是判断太阳在黄道上的运行轨迹,这个很高深,我也是找了网上大牛写的源码。 有了公历日期和农历日期,计算节日就方便了。公历节日多是某月某日,而农历节日多是某月初几或十五,直接套日期便是,还有就是二十四节气里的节日如清明和冬至等等。有几个西方节日比较特殊,要算某月的第几个星期几,比如父亲节是五月的第二个星期日,母亲节是六月的第三个星期日,感恩节是七月的第四个星期四等等,这里给出这几个节日的代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static int getWeekday(Date date){
int dayOfWeek = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
return dayOfWeek;
}
final static String[] weekHoliday = new String[]{"0527 母亲节", "0637 父亲节", "0744 感恩节"};
public static void main(String[] arg) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
int year = 2015;
for (int month=1; month<=12; month++) {
int day_num = 31;
if (month == 2) {
day_num = 28;
} else if (month==4 || month==6 || month==9 || month==11) {
day_num = 30;
}
for (int day=1; day<=day_num; day++) {
for(int k = 0; k < weekHoliday.length; k++){
int weeknum = (day-1) / 7;
if (day-weeknum*7 > 0) {
weeknum++;
}
Date date = sdf.parse(String.format("%d%02d%02d", year, month, day));
//返回公历节假日名称
String wd = weekHoliday[k].split(" ")[0]; //节假日的日期
String wdv = weekHoliday[k].split(" ")[1]; //节假日的名称
String wmd = String.format("%02d%d%d", month, weeknum, getWeekday(date));
if(wd.equals(wmd)){
System.out.printf("%d年%d月%d日是%s\n", year, month, day, wdv);
}
}
}
}
}
}