做了一段时间的Java开发,总感觉日期的处理有点吃力,抽了个时间把自己用过的处理方法贴出来,有空就可以看看了,也不用因为积分不够而没有下载资源。呵呵,有错误的地方当然希望大家帮我指出来,谢谢
package com.wyebd.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
public class DateTest {
//获取当前系统时间system.currentTimeMillis()是long型,转换Date
public static String getDates(long millis){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
Formatter ft = new Formatter(java.util.Locale.CHINA);
return ft.format("%1$tY年%1$tm月%1$td日%1$tT %1$tA %1$tp", cal).toString();
}
//根据日期 传参获取相应日期
public static String getSpecifiedDayAfter(String dates,int n){
Calendar c=Calendar.getInstance();
Date date;
try {
date = new java.text.SimpleDateFormat("yyyy年MM月dd日").parse(dates);
c.setTime(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day+n);
String dayAfter = new java.text.SimpleDateFormat("yyyy年MM月dd日").format(c.getTime());
return dayAfter;
}
//日期转换周几
public static String parseDate(String theDate){
String strReturn="";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy年MM月dd日");
try {
Date date = sdf.parse(theDate);
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("EEEE");
strReturn=sdf1.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strReturn;
}
/**
* 时间str to date,date to str
* Bing
*/
public static String strTodate(String dTime) throws Exception{
dTime="2013-6-15 15:30:00";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse(dTime);//str to date
dTime=getSpecifiedDayAfter(sdf.format(date1),-3);// 指定日期前一天 sdf.format(date1) date to str
/*dTime=getSpecifiedDayAfter(sdf.format(date1));// 指定日期后一天 sdf.format(date1) date to str*/
// System.out.println(beginTim+"--------before-|-----|-end----------"+endTim);
return dTime;
}
/**
* 时间long to date,date to long
* Bing
*/
public static String longTodate(String dTime) throws Exception{
dTime="2013-6-15 15:30:00";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeStart=sdf.parse("2011-09-20 12:30:45").getTime();
System.out.println(timeStart);//date to long
Date date = new Date(timeStart);
System.out.println(sdf.format(date));//long todate
return dTime;
}
/**
* 获取当前系统时间并做一些处理
* @Auto Bing
* @param args
*/
public static void main(String[] args) {
//java.util.Calendar;
Calendar cal = Calendar.getInstance();
String Tim= cal.get(Calendar.YEAR)+"年"+ (cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DATE)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
System.out.println(Tim+"-------------------------------------------------------------------------------------------------------------------");
//java.text.DateFormat
Date now =new Date();//获取当前系统时间1
DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);//日期格式 比如:2013-8-2
String str2=DateFormat.getDateTimeInstance().format(now); //精确到秒
String str3=DateFormat.getTimeInstance().format(now);//精确到秒
String str4=DateFormat.getInstance().format(now); //使用SHORT风格显示日期和时间 12小时,分上、下午
String str5=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(now);//显示日期,周,时间(精确到秒)
String str6=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(now);//显示日期。时间(精确到秒)
String str7=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now);//显示日期。时间(精确到分)
String str8=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(now);//显示日期。时间(精确到分)与SHORT风格相比,这种方式最好用
System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);
System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);
//获取当前系统时间system.currentTimeMillis()
System.out.println("System.currentMillis()="+getDates(System.currentTimeMillis()));
//根据SimpleDateFormat将获取到的Date()转换成一般格式
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
System.out.println("SimpleDateFormat将获取到的Date()转换成一般格式:"+sdf.format(now));
//后一天、后n天 往后推指定天数 0表示当天 负数表示前推
System.out.println("**********************dayAfter:"+getSpecifiedDayAfter("2013年8月2日",3));//往后推3天
System.out.println("**********************dayCurrent:"+getSpecifiedDayAfter("2013年8月2日",0));//当天
System.out.println("**********************dayBefore:"+getSpecifiedDayAfter("2013年8月2日",-3));//往前推推3天
//指定日期转换周天
System.out.println("**********************今天是:"+parseDate(getSpecifiedDayAfter("2013年8月2日",0)));//当天
}
/**类似插件的日期处理
*/
/**
* 根据日期 传参获取相应日期
*/
public static String getSpecifiedDayAfter(String dates,int n) throws ParseException{
Calendar c=Calendar.getInstance();
Date date = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dates);
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day+n);
String dayAfter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(c.getTime());
return dayAfter;
}
public static void main(String[] args) throws ParseException{
// /**
// * 网上自定义日历控件
// */
// GregorianCalendar d = new GregorianCalendar();
// int today = d.get(Calendar.DAY_OF_MONTH);
// int month = d.get(Calendar.MONTH);
// d.set(Calendar.DAY_OF_MONTH, 1);
// int weekday = d.get(Calendar.DAY_OF_WEEK);
// System.out.println("Sun Mon Tue Wed Thu Fri Sat");
// for(int i=Calendar.SUNDAY; i<weekday; ++i)
// System.out.print(" ");
// do
// {
// int day = d.get(Calendar.DAY_OF_MONTH);
// if(day < 10) System.out.print(" ");
// System.out.print(day);
// if(day == today)
// System.out.print("* ");
// else
// System.out.print(" ");
// if(weekday == Calendar.SATURDAY)
// System.out.println();
// d.add(Calendar.DAY_OF_MONTH, 1);
// weekday = d.get(Calendar.DAY_OF_WEEK);
// }while(d.get(Calendar.MONTH) == month);
// if(weekday != Calendar.SUNDAY)
// System.out.println();
//
// /**
// *只要通过向SimpleDateFormat 的构造函数传递格式字符串"EEE-MMMM-dd-yyyy", 我们就能够指明自己想要的格式.
// *你应该可以看见, 格式字符串中的ASCII 字符告诉格式化函数下面显示日期数据的哪一个部分. EEEE是星期, MMMM是月,
// *dd是日, yyyy是年. 字符的个数决定了日期是如何格式化的.传递"EE-MM-dd-yy"会显示 Sat-09-29-01
// */
// SimpleDateFormat bartDateFormat =
new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
new SimpleDateFormat("EEEE-M-d-yyyy");
// new SimpleDateFormat("yyyy-MM-dd 是 EEEE");
// Date date = new Date();
// System.out.println(bartDateFormat.format(date));
// /**
// *
// */
// SimpleDateFormat sdf=new SimpleDateFormat("EEEE yyyy-MM-dd HH:mm:ss");
// String dTime=sdf.format(System.currentTimeMillis()+3*1000*60*60*24).toString();//获取系统时间
// String now=new Date().toLocaleString();
// String before=new Date(new Date().getTime()-1000*60*60*24).toLocaleString();
// System.out.println(dTime+"---"+now+"---"+sdf.format(new Date())+"---"+before+"---"+new Date().getDay()+"==="+sdf.parse(dTime).getMonth()+"--相差整天--"+getSpecifiedDayAfter(before,-3));
// Calendar calendar = Calendar.getInstance();
// SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
// String s = df.format(calendar.getTime());
// System.out.println(s);
// System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1); //Calendar获取周几会提前一天
DateFormat d1 = DateFormat.getDateTimeInstance();
System.out.println(d1.format(new Date()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
Date date =new Date();
String timeCurr = s.format(date);
String time=sdf.format(date);
System.out.println(time+"--"+timeCurr+"==="+String.format("%.0f", Double.parseDouble("100")));//自带String属性方法保留小数位数
}
}