Clander---date用法.docVIP

  • 28
  • 0
  • 约2.79万字
  • 约 25页
  • 2018-02-19 发布于河南
  • 举报
Clander---date用法

package com.fengzhiyin.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * @classname: 日期处理类 * @author: Fengzhiyin * @time: 2009-07-23 */ @SuppressWarnings(static-access) public final class DateUtil { /** * 得到当前日期的月首 格式为:2009-08-01 */ public static String monthFist() { Calendar localTime = Calendar.getInstance(); String strY = null;// 日期属性:日 int x = localTime.get(Calendar.YEAR); // 日期属性:年 int y = localTime.get(Calendar.MONTH) + 1; // 日期属性:月 strY = y = 10 ? String.valueOf(y) : (0 + y); // 组合月份 return x + - + strY + -01; // 最后组合成yyyy-mm-dd形式字符串 } /** * 得到上个月月首 格式为:2009-08-01 */ public static String beforeMonth() { Calendar localTime = Calendar.getInstance(); localTime.add(Calendar.MONTH, -1); // 通过提取这个月计算上个月号 String strz = null; int x = localTime.get(Calendar.YEAR); // 得到年 int y = localTime.get(Calendar.MONTH) + 1; // 得到月 strz = y = 10 ? String.valueOf(y) : (0 + y); return x + - + strz + -01; } /** * 得到当前日期 格式为:2009-08-01 */ public static String curDate() { // 分别根据日历时间提取当前年月日组合成字符串 Calendar localTime = Calendar.getInstance(); int x = localTime.get(Calendar.YEAR); int y = localTime.get(Calendar.MONTH) + 1; int z = localTime.get(Calendar.DAY_OF_MONTH); return x + - + y + - + z; } /** * 给定的日期加一个月 格式为:2009-08-01 */ public static String addMonth(String strdate) { Date date = new Date(); // 构造一个日期型中间变量 String dateresult = null; // 返回的日期字符串 // 创建格式化格式 SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd); // 加减日期所用 GregorianCalendar gc = new GregorianCalendar(); try { date = df.parse(strdate); // 将字符串格式化为日期型 } catch (ParseException e) { e.printStackTrace(); } gc.setTime(date); // 得到gc格式的时间 gc.add(2, 1); // 2表示月的加减,年代表1依次类推(周,天。。) // 把运算完的时间从新赋进对象 gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE)); // 在格式化回字符串时间 dateresult

文档评论(0)

1亿VIP精品文档

相关文档