时间处理
来源:互联网 发布:mac压缩包密码破解 编辑:程序博客网 时间:2024/06/03 23:34
public class FDateFormat { public static final SimpleDateFormat NORMAL_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static final SimpleDateFormat NORMAL_FORMATA = new SimpleDateFormat("yyyy-MM-dd"); public static final SimpleDateFormat NORMAL_FORMATB = new SimpleDateFormat("yyyyMMdd"); public static final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy"); public static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("MM"); public static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("dd"); public static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("HH"); public static final SimpleDateFormat MINUTE_FORMAT = new SimpleDateFormat("mm"); public static final SimpleDateFormat SECOND_FORMAT = new SimpleDateFormat("ss"); /*** * 功能:返回 yyyy-MM-dd HH:mm:ss 格式的日期 * @return */ public static String GetLongDate(){ String strRet=""; try{ SimpleDateFormat sf=NORMAL_FORMAT; strRet=sf.format(new java.util.Date(System.currentTimeMillis())); }catch(Exception e){ e.printStackTrace(); } return strRet; } /*** * 功能:返回 yyyy-MM-dd 格式的日期 * @return */ public static String GetShortDate(){ String strRet=""; try{ SimpleDateFormat sf=NORMAL_FORMATA; strRet=sf.format(new java.util.Date(System.currentTimeMillis())); }catch(Exception e){ e.printStackTrace(); } return strRet; } /*** * 返回yyyyMMdd 8位格式的年月日 * @param yearData(格式:yyyy-MM-dd) * @return */ public static String GetDateString(){ String strRet=""; try{ SimpleDateFormat sf=NORMAL_FORMATB; strRet=sf.format(new java.util.Date(System.currentTimeMillis())); }catch(Exception e){ e.printStackTrace(); } return strRet; } //返回当前日期的格式 public static String GetDateTime(SimpleDateFormat sformat) { SimpleDateFormat sf=null; String strRet=null; if(sformat==null) { sf=NORMAL_FORMAT; } else { sf=sformat; } try { strRet=sf.format(new java.util.Date(System.currentTimeMillis())); } catch(Exception e) { System.out.println("getCurrent time error="+e.getMessage()); } return strRet; } /**** * 获取当前日期的后多少天 * @param days * @return */ public static String GetDateAfterNow(int days){ Calendar curdate = Calendar.getInstance(); curdate.add(Calendar.DAY_OF_MONTH,days);// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(curdate.getTime()).toString(); } /*** * 计算传入日期多少天后的日期或多少天前的日期 * @param inputDate---格式:yyyy-MM-dd * @param days--------天数差 * @return */ public static String GetDateByInputPara(String inputDate,int days){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date dateTemp=formatter.parse(inputDate, new ParsePosition(0)); Calendar curdate = Calendar.getInstance(); curdate.setTime(dateTemp); //要加减的日期 curdate.add(Calendar.DATE, days); return formatter.format(curdate.getTime()).toString(); } /**** * 获取从今年开始的多少年 * @param years * @return */ public static String getYearFromNow(int years){ Calendar curdate = Calendar.getInstance(); curdate.add(Calendar.YEAR,years); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(curdate.getTime()).toString(); } /*** * 计算当前年份与传入年份的差 * @param yearData(格式:yyyy-MM-dd) * @return */ public static int getYearDiffer(String yearData){ String yearString=yearData.substring(0, 4); Calendar curdate = Calendar.getInstance(); int retDiff=curdate.get(Calendar.YEAR)-Integer.parseInt(yearString); return retDiff; } /*** * 获取月天数 * @param yearData(格式:yyyy-MM) * @return */ public static int getYearDiffer(String yearData){ Calendar rightNow = Calendar.getInstance(); SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM"); rightNow.setTime(simpleDate.parse(yearData));//传入计算的年月份 int days=rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);//获取传入月的天数 return days; } public static void main(String[] args){ System.out.println(getYearDiffer("1980-12-23")); System.out.println(GetDateByInputPara("1980-12-23",-3)); }}
0 0
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 时间处理
- 多播包的发送与接收
- Leetcode Q4:Median of Two Sorted Arrays
- iOS 编程 利用UIScrollView 编写无缝循环显示图片
- 黑马程序员_交通灯系统
- 数据库题——高于部门平均工资查询问题
- 时间处理
- Elevator
- vc6.0下,GDI+的配置
- Highcharts数据表示(2)
- HDU 1850 Being a Good Boy in Spring Festival(Nim博弈)
- hdu 1142 A Walk Through the Forest
- 数电模电基础(1)分压电路
- mybatis 参数为String,if test读取该参数
- C#excel、sql sever批量数据的导入导出