我是
靠谱客的博主
大意枕头,最近开发中收集的这篇文章主要介绍
日期工具类DateUtils,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
- package date.util;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- public class DateUtils {
-
- static final String formatPattern = "yyyy-MM-dd";
-
- static final String formatPattern_Short = "yyyyMMdd";
-
-
-
-
-
-
- public static String getCurrentDate(){
- SimpleDateFormat format = new SimpleDateFormat(formatPattern);
- return format.format(new Date());
- }
-
-
-
-
-
-
- public static String getDesignatedDate(long timeDiff){
- SimpleDateFormat format = new SimpleDateFormat(formatPattern);
- long nowTime = System.currentTimeMillis();
- long designTime = nowTime - timeDiff;
- return format.format(designTime);
- }
-
-
-
-
-
- public static String getPrefixDate(String count){
- Calendar cal = Calendar.getInstance();
- int day = 0-Integer.parseInt(count);
- cal.add(Calendar.DATE,day);
- Date datNew = cal.getTime();
- SimpleDateFormat format = new SimpleDateFormat(formatPattern);
- return format.format(datNew);
- }
-
-
-
-
-
- public static String dateToString(Date date){
- SimpleDateFormat format = new SimpleDateFormat(formatPattern);
- return format.format(date);
- }
-
-
-
-
-
- public static Date stringToDate(String str){
-
- SimpleDateFormat format = new SimpleDateFormat(formatPattern);
- if(!str.equals("")&&str!=null){
- try {
- return format.parse(str);
- } catch (ParseException e) {
-
- e.printStackTrace();
- }
- }
- return null;
- }
-
-
- public void timeSubtract(){
- SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date begin = null;
- Date end = null;
- try {
- begin = dfs.parse("2004-01-02 11:30:24");
- end = dfs.parse("2004-03-26 13:31:40");
- } catch (ParseException e) {
- e.printStackTrace();
- }
-
- long between = (end.getTime() - begin.getTime()) / 1000;
-
- long day1 = between / (24 * 3600);
- long hour1 = between % (24 * 3600) / 3600;
- long minute1 = between % 3600 / 60;
- long second1 = between % 60;
- System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分"
- + second1 + "秒");
- }
-
- }
最后
以上就是大意枕头为你收集整理的日期工具类DateUtils的全部内容,希望文章能够帮你解决日期工具类DateUtils所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复