首页 / JAVA / java日期时间操作
java日期时间操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java日期时间操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6178字,纯文字阅读大概需要9分钟。
内容图文
package com.gds.web.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateOpUtil { /** * @param args * @throws ParseException */ public static void main(String[] args) throws Exception { System.out.println(daysBetween("2015-09-28 10:10:10","2015-10-01 23:00:00")); } /* *日期格式转换 yyyy-MM-dd */ public static String turnDateFormat(String date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(format.parse(date)); } /* * 两日期比较时间 */ public static boolean dateEqual(String begin_date,String end_date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = format.parse(begin_date); Date d2 = format.parse(end_date); if(d1.getTime() == d2.getTime()) { return true ; } return false; } /* * 两日期比较时间 */ public static boolean dateDef(String now_date,String use_date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date now_time = format.parse(now_date); Date end_time = format.parse(use_date); if(end_time.getTime() >= now_time.getTime()) { return true ; } return false; } /** * 计算两日期相隔天数 *字符串的日期格式的计算 */ public static int daysBetween(String smdate,String bdate) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /** * 获得指定日期的后一天 * * @param specifiedDay * @return * @throws Exception */ public static String getSpecifiedDayBefore(String specifiedDay,int i) {//可以用new Date().toLocalString()传递参数 Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day + i); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } /** * 判断当前日期是星期几 * * @param pTime 修要判断的时间 * @return dayForWeek 判断结果 * @Exception 发生异常 */ public static int dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if(c.get(Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; } }
package com.gds.web.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.StringUtils; import com.ctc.wstx.util.StringUtil; /** * @author microe * * */ public class FixedDate { /** * 获得指定日期的前一天 * * @param specifiedDay * @return * @throws Exception */ public static String getSpecifiedDayBefore(String specifiedDay,int i) {//可以用new Date().toLocalString()传递参数 Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - i); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } /* * 获取指定日期,本周的周一日期 */ public static String getMondayOfThisWeek(String fixed_date) throws Exception { DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date); int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1; if (day_of_week == 0) day_of_week = 7; c.add(Calendar.DATE, -day_of_week + 1); return fmt.format(c.getTime())+ " 00:00:00"; } /* * 获取指定日期,本月的一号日期 */ public static String getDateOfThisMonth(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); //获取当前月第一天: Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 return fmt.format(c.getTime())+ " 00:00:00"; } /** * 当前季度的开始时间,即2012-01-1 00:00:00 * * @return */ public static String getCurrentQuarterStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); return fmt.format(c.getTime()) + " 00:00:00"; } //获取上半年、下半年开始时间 public static String getHalfYearStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); int currentMonth = c.get(Calendar.MONTH) + 1; String date = null; try { if (currentMonth >= 1 && currentMonth <= 6){ c.set(Calendar.MONTH, 0); }else if (currentMonth >= 7 && currentMonth <= 12){ c.set(Calendar.MONTH, 6); } c.set(Calendar.DATE, 1); date = fmt.format(c.getTime())+ " 00:00:00"; } catch (Exception e) { e.printStackTrace(); } return date; } /** * 当前年的开始时间,即2012-01-01 00:00:00 * * @return */ public static String getCurrentYearStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); c.set(Calendar.MONTH, 0); c.set(Calendar.DATE, 1); return fmt.format(c.getTime())+ " 00:00:00"; } public static void main(String[] args) throws Exception { System.out.println( getCurrentQuarterStartTime("2015-09-09") ); } }
原文:http://microe.blog.51cto.com/3332651/1698238
内容总结
以上是互联网集市为您收集整理的java日期时间操作全部内容,希望文章能够帮你解决java日期时间操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】