betway必威-betway必威官方网站
做最好的网站

时间格式化问题,时间校验

 

java.util.*;
import java.text.*;
import java.util.Calendar;

DateUtil时间工具类,dateutil工具类

package com.ruisi.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateUtil{

/** 暗许的岁月日 */
public static final String defaultPattern = "yyyy-MM-dd";

/** hour12HMSPattern年月日 时分秒 12小时制*/
public static final String hour12HMSPattern = "yyyy-MM-dd hh:mm:ss";

/** hour12HMPattern年月日 时分 12小时制*/
public static final String hour12HMPattern = "yyyy-MM-dd hh:mm";

/** hour12HPattern年月日 时 12小时制*/
public static final String hour12HPattern = "yyyy-MM-dd hh";

/** hour24HMSPattern年月日 时分秒 24小时制 */
public static final String hour24HMSPattern = "yyyy-MM-dd HH:mm:ss";

/** hour24HMPattern年月日 时分 24小时制*/
public static final String hour24HMPattern = "yyyy-MM-dd HH:mm";

/** hour24HPattern年月日 时 24小时制*/
public static final String hour24HPattern = "yyyy-MM-dd HH";

public static String daysBetween(Date startDate, Date endDate) {
float d = endDate.getTime() - startDate.getTime();
float dd = d / 86400000f;
int ddd = (int)dd;

float hh = (dd - ddd) * 24;
int hhh = (int)hh;

float mm = (hh - hhh) * 60;
int mmm = (int)mm;

float ss = (mm - mmm) * 60;
int sss = (int)ss;
return ddd "天" hhh "小时" mmm "分" sss "秒";
}

/**
* 再次来到预设Format的当前日期字符串
*/
public static String getToday() {
Date today = new Date();
return format(today);
}

public static String getYestoday() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -1);
return format(cal1.getTime());
}

public static String getTheDayBeforeYestoday() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -2);
return format(cal1.getTime());
}

public static String getPreviousDay3() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -3);
return format(cal1.getTime());
}

public static String getPreviousDay4() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -4);
return format(cal1.getTime());
}

/**
* 用预设Format格式化Date成字符串
*/
public static String format(Date date) {
return format(date, defaultPattern);
}

/**
* 使用参数Format格式化Date成字符串
*/
public static String format(Date date, String pattern) {
String returnValue = "";

if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}

return (returnValue);
}

/**
* 使用预设格式将字符串转为Date
*/
public static Date parse(String strDate) throws ParseException {
return parse(strDate, defaultPattern);
}

public static Date parse(String strDate, String pattern)
throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
}

/**
* 在日期上增添数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}

/**
* 在日期上扩张日
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}

public static String formatDateTime(Date date) {
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return outFormat.format(date);
}

@SuppressWarnings("static-access")
public static String getEndOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
cal.set(cal.YEAR, Integer.parseInt(year));
cal.set(cal.MONTH, Integer.parseInt(month) - 1);
return cal.getActualMaximum(cal.DAY_OF_MONTH) "";
}

public static String addDays(String sdate, int n) throws ParseException
{
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parse(sdate, defaultPattern));
cal1.add(Calendar.DATE, n);
return format(cal1.getTime());

}

public static String getFirstOfMonth() throws ParseException {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.set(5, 1);
return format(cal1.getTime());

}

public static String getFirstOfMonth(String sDate) throws ParseException {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parse(sDate, defaultPattern));
cal1.set(5, 1);
return format(cal1.getTime());

}

/**
* 获取年
*
* @param sdate
* @return String
*/
public static String getYear(String sdate) {
String[] date = sdate.split("-");
return date[0];
}

/**
* 获取月
*
* @param sdate
* @return String
*/
public static String getMonth(String sdate) {
String[] date = sdate.split("-");
return date[1];
}

public static String getCurrentYear() {
Calendar cale = Calendar.getInstance();
return Integer.toString(cale.get(Calendar.YEAR));
}

public static String getCurrentMonth() {
Calendar cale = Calendar.getInstance();
int month = cale.get(Calendar.MONTH);
month ;
String sMonth = Integer.toString(month);
if (month < 10)
sMonth = "0" month;
return sMonth;
}

/**
* 获取天
*
* @param sdate
* @return String
*/
public static String getDay(String sdate) {
String[] date = sdate.split("-");
return date[2];
}

public static String getFullDate(String date) {
if (date != null && date.length() == 1)
return "0" date;
return date;
}

public static String getSimpleDateString(String sdate) {
return sdate.replace("-", "");
}

//把日子从字符弄转成日期型
public static Date convertStringToDate(String pattern, String strDate)
throws ParseException {
Date aDate = null;
aDate = parse(strDate, pattern);
return aDate;
}

//依据钦点格式得到当明天子的字符串
public static String getTodayDate(String aMask){
Date date = new Date();
return getDateTime(aMask,date);
}

//依照内定格式得到钦命日期的字符串
public static String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
return (returnValue);
}

public static int getYear() {
Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
return yy;
}

public static int getMonth() {
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
return month 1;
}

public static int getDate() {
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
return date;
}

public static int getHour() {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
return hour;
}

public static int getSecond() {
Calendar c = Calendar.getInstance();
int second = c.get(Calendar.SECOND);
return second;
}

public static int getMinute() {
Calendar c = Calendar.getInstance();
int minute = c.get(Calendar.MINUTE);
return minute;
}

public static String getCurDateTime() {
return getCurDate() " " getCurTime();
}

public static String getCurDate() {
String fullDate = getCurYearMonth();
int temp = getDate();
if(temp < 10){
fullDate = "-0" temp;
}else{
fullDate = "-" temp;
}
return fullDate;
}

public static String getCurYearMonth() {
String fullDate = String.valueOf(getYear());
int temp = getMonth();
if(temp < 10){
fullDate = "-0" temp;
}else{
fullDate = "-" temp;
}
return fullDate;
}

public static String getCurTime() {
String time = getCurHourMinute();
int temp = getSecond();
if(temp < 10){
time = ":0" temp;
}else{
time = ":" temp;
}
return time;
}

public static String getCurHourMinute() {
String time;
int temp = getHour();
if(temp < 10){
time = "0" temp ":";
}else{
time = temp ":";
}
temp = getMinute();
if(temp < 10){
time = "0" temp;
}else{
time = temp;
}
return time;
}
/**
*
* @Description: 获取当前岁月
* @param @return
* @return StringBuffer
* @throws
* @author ningpeng
* @date 2016年1月8日
*/
public static StringBuffer getNow()
{
Date date = new Date();
Calendar c = new GregorianCalendar();
c.setTime(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) 1;
int day = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
StringBuffer buff = new StringBuffer();
buff.append(year).append(month).append(day).append(hour).append(minute).append(seconds).toString();
return buff;
}

public static String DateFormat(Date date) {
Calendar c=Calendar.getInstance();
c.setTime(date);
c.get(java.util.Calendar.HOUR_OF_DAY);
String pattern="yyyy-MM-dd";
if(c.get(java.util.Calendar.HOUR_OF_DAY)!=0){
pattern="yyyy-MM-dd hh";
}else if(c.get(java.util.Calendar.MINUTE)!=0){
pattern="yyyy-MM-dd hh:mm";
}else if(c.get(java.util.Calendar.SECOND)!=0){
pattern="yyyy-MM-dd hh:mm:ss";
}
return new SimpleDateFormat(pattern).format(date);
}

public static void main(String[] args){
System.out.println(getMonth());
}
}

package com.ruisi.core.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java...

 

public class VeDate {
 /**
  * 获取将来时刻
  * 
  * @return 重回时间档次 yyyy-MM-dd HH:mm:ss
  */
 public static Date getNowDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
 }

public static boolean checkValidDate(String pDateObj) {   

 /**
  * 获取未来岁月
  * 
  * @return重回短期格式 yyyy-MM-dd
  */
 public static Date getNowDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
 }

        try{   

 /**
  * 获取今后光阴
  * 
  * @return再次回到字符串格式 yyyy-MM-dd HH:mm:ss
  */
 public static String getStringDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

          int year = new Integer(pDateObj.substring(0, 4)).intValue();   

 /**
  * 获取今后时刻
  * 
  * @return 重临长期字符串格式yyyy-MM-dd
  */
 public static String getStringDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

          int month = new Integer(pDateObj.substring(4, 6)).intValue();   

 /**
  * 获取时间 时辰:分;秒 HH:mm:ss
  * 
  * @return
  */
 public static String getTimeShort() {
  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  Date currentTime = new Date();
  String dateString = formatter.format(currentTime);
  return dateString;
 }

          int date = new Integer(pDateObj.substring(6, 8)).intValue(); 

 /**
  * 将长日子格式字符串调换为时间 yyyy-MM-dd HH:mm:ss
  * 
  * @param strDate
  * @return
  */
 public static Date strToDateLong(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
 }

          int hourOfDay = new Integer(pDateObj.substring(8, 10)).intValue(); 

 /**
  * 将长日子格式时间更改为字符串 yyyy-MM-dd HH:mm:ss
  * 
  * @param dateDate
  * @return
  */
 public static String dateToStrLong(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(dateDate);
  return dateString;
 }

          int minute = new Integer(pDateObj.substring(10, 12)).intValue(); 

 /**
  * 将短期格式时间转移为字符串 yyyy-MM-dd
  * 
  * @param dateDate
  * @param k
  * @return
  */
 public static String dateToStr(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(dateDate);
  return dateString;
 }

          int second = new Integer(pDateObj.substring(12, 14)).intValue(); 

 /**
  * 将短期格式字符串转变为时间 yyyy-MM-dd 
  * 
  * @param strDate
  * @return
  */
 public static Date strToDate(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
 }

          Calendar   cal   =   Calendar.getInstance();   

 /**
  * 得到以以前子
  * 
  * @return
  */
 public static Date getNow() {
  Date currentTime = new Date();
  return currentTime;
 }

          cal.setLenient(   false   );   //允许严刻检查日期格式   

 /**
  * 提取一个月底的最终一天
  * 
  * @param day
  * @return
  */
 public static Date getLastDate(long day) {
  Date date = new Date();
  long date_3_hm = date.getTime() - 3600000 * 34 * day;
  Date date_3_hm_date = new Date(date_3_hm);
  return date_3_hm_date;
 }

          cal.set(year,   month-1,   date); 

 /**
  * 得到以后时光
  * 
  * @return 字符串 yyyyMMdd HHmmss
  */
 public static String getStringToday() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

          cal.set(year, month - 1, date, hourOfDay, minute, second); 

 /**
  * 得到未来时辰
  */
 public static String getHour() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  String hour;
  hour = dateString.substring(11, 13);
  return hour;
 }

本文由betway必威发布于编程开发,转载请注明出处:时间格式化问题,时间校验

TAG标签: betway必威
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。