FormatDateTime.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:6k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
Java
- /*
- * Created on 2005-6-28
- * Made In GamVan
- */
- package com.gamvan.tools;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- public class FormatDateTime {
- /**
- * 日期类操作工具
- * @author GamVan by 我容易么我
- * Powered by GamVan.com
- */
- public static String formatDateTime(String ymd){
- //格式化当前时间
- java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
- String now = isNow.format(new java.util.Date());
- return now;
- }
- public static String formatDateTime(String ymd, String datetime){
- //格式化当前时间
- java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
- String now = "";
- try{
- isNow.format(datetime);
- }catch(Exception e){
- e.printStackTrace();
- }
- return now;
- }
- /* 比较当前日期和指定日期 return boolean
- * 如果当前日期在指定日期之后返回true否则返回flase
- */
- public static boolean dateCompare(String str){
- boolean bea = false;
- SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
- String isDate = sdf_d.format(new java.util.Date());
- java.util.Date date1;
- java.util.Date date0;
- try {
- date1 = sdf_d.parse(str);
- date0= sdf_d.parse(isDate);
- if(date0.after(date1)){
- bea = true;
- }
- } catch (ParseException e) {
- bea = false;
- }
- return bea;
- }
- /*
- * 比较当前月份和指定月份
- * 如果当前月份在指定月份之后返回true否则返回flase
- */
- public static boolean monthCompare(String str){
- boolean bea = false;
- SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
- String isMonth = sdf_m.format(new java.util.Date());
- java.util.Date date1;
- java.util.Date date0;
- try {
- date1 = sdf_m.parse(str);
- date0= sdf_m.parse(isMonth);
- if(date0.after(date1)){
- bea = true;
- }
- } catch (ParseException e) {
- bea = false;
- }
- return bea;
- }
- /* 比较当前日期和指定日期 return boolean
- * 如果当前日期在指定日期之后返回true否则返回flase
- */
- public static boolean secondCompare(String str){
- boolean bea = false;
- SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String isDate = sdf_d.format(new java.util.Date());
- java.util.Date date1;
- java.util.Date date0;
- try {
- date1 = sdf_d.parse(str);
- date0= sdf_d.parse(isDate);
- if(date0.after(date1)){
- bea = true;
- }
- } catch (ParseException e) {
- bea = false;
- }
- return bea;
- }
- /**
- * 比较指定两日期如果str1晚于str2则return false; 精确到秒
- * @param str1
- * @param str2
- * @return
- * 2005-11-7 5:26:35 Made In GamVan
- * com.gamvan.tools
- */
- public static boolean secondCompare(String str1, String str2){
- boolean bea = false;
- SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- java.util.Date date1;
- java.util.Date date0;
- try {
- date1 = sdf_d.parse(str1);
- date0= sdf_d.parse(str2);
- if(date0.after(date1)){
- bea = true;
- }
- } catch (ParseException e) {
- bea = false;
- }
- return bea;
- }
- /**
- * 比较指定两日期如果str1晚于str2则return flase; 精确到天
- * @param str1
- * @param str2
- * @return
- * 2005-11-7 5:26:35 Made In GamVan
- * com.gamvan.tools
- */
- public static boolean dateCompare(String str1, String str2){
- boolean bea = false;
- SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
- java.util.Date date1;
- java.util.Date date0;
- try {
- date1 = sdf_d.parse(str1);
- date0= sdf_d.parse(str2);
- if(date0.after(date1)){
- bea = true;
- }
- } catch (ParseException e) {
- bea = false;
- }
- return bea;
- }
- /**
- * 设置间隔数后返回时间
- * @param type
- * @param i
- * @return
- * 2005-11-7 5:24:36 Made In GamVan
- * com.gamvan.tools
- */
- public static String dateAdd(String type, int i){
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = formatDateTime("yyyy-MM-dd HH:mm:ss");
- Calendar c = Calendar.getInstance(); // 当时的日期和时间
- if(type.equals("s")){
- int s = c.get(Calendar.SECOND);
- s = s + i;
- c.set(Calendar.SECOND, s);
- str = df.format(c.getTime());
- }
- else if(type.equals("d")){
- int d = c.get(Calendar.DAY_OF_MONTH); // 取出“日”数
- d = d + i;
- c.set(Calendar.DAY_OF_MONTH, d); // 将“日”数设置回去
- str = df.format(c.getTime());
- }
- return str;
- }
- public static void main(String args[]){
- //String s1 = FormatDateTime.formatDateTime("yyyy-MM-dd","2005-10-12");
- //System.out.println(s1);
- //System.out.print(FormatDateTime.secondCompare("2005-11-06 00:00:00","2005-12-06 00:00:00"));
- //System.out.print(FormatDateTime.dateCompare("2005-11-06","2005-10-06"));
- System.out.print(FormatDateTime.dateAdd("d",-1));
- }
- }