java -类型转化-(Date&&String):
package com.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Date 2020/11/19 15:42
* @CREATE BY XIAOQI
* @DESCRIPTION String 类型 转化为 Date 类型
* @MODIFIED
*/
public class FDate {
public Date getDates(String D) {
Date date = null;
SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd");
try {
date = si.parse(D);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 得到当前系统时间 author: YT
*
* @return 当前时间的格式字符串,时间格式为"HH:mm:ss"
*/
public static String getCurrentTime() {
String pattern = "HH:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
* 输入符合格式要求的日期字符串,返回日期类型变量
* <p><b>Example: </b><p>
* <p>getDate("2002-10-8") returns "Tue Oct 08 00:00:00 CST 2002"<p>
*
* @param dateString 日期字符串
* @return 日期类型变量
*/
private static final String pattern = "yyyy-MM-dd";
private static final String pattern1 = "yyyyMMdd";
private static SimpleDateFormat df = new SimpleDateFormat(pattern);
private static SimpleDateFormat df1 = new SimpleDateFormat(pattern1);
public Date getDate(String dateString) {
Date tDate = null;
try {
if (dateString.indexOf("-") != -1) // 如果 = -1 值表明:字符串dateSting中不存在 "-" 符号。反之存在!
{
tDate = df.parse(dateString);
} else {
tDate = df1.parse(dateString);
}
} catch (Exception e) {
e.printStackTrace();
}
return tDate;
}
/**
* 输入日期类型变量,返回日期字符串
*
* <p>getString("Tue Oct 08 00:00:00 CST 2002") returns "2002-10-8"<p>
*
* @param mDate 日期类型变量
* @return 日期字符串
*/
public String getString(Date mDate) {
String tString = null;
if (mDate != null) {
tString = df.format(mDate);
}
return tString;
}
}
package com.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class FDateM {
/**
* 计算日期的函数: calDate()
*
* 函数原型:
* public static Date calDate(Date baseDate, int interval, String unit,Date compareDate)
*
* 参数说明:
* @param baseDate 起始日期
* @param interval 时间间隔
* @param unit 时间间隔单位
* @param compareDate 参照日期
* @return Date类型变量
*
* 功能说明:参照日期指当按照年月进行日期的计算的时候,参考的日期,如下例,结果返回2002-03-31 zijisuan:2000-03-31
*/
public static Date calDate(Date baseDate, int interval, String unit,
Date compareDate) {
Date returnDate = null;
GregorianCalendar mCalendar = new GregorianCalendar();
mCalendar.setTime(baseDate);
if (unit.equals("Y")) {
mCalendar.add(Calendar.YEAR, interval);
}
if (unit.equals("M")) {
mCalendar.add(Calendar.MONTH, interval);
}
if (unit.equals("D")) {
mCalendar.add(Calendar.DATE, interval);
}
if (compareDate != null) {
GregorianCalendar cCalendar = new GregorianCalendar();
cCalendar.setTime(compareDate);
int mYears = mCalendar.get(Calendar.YEAR);
int mMonths = mCalendar.get(Calendar.MONTH);
int cMonths = cCalendar.get(Calendar.MONTH);
int cDays = cCalendar.get(Calendar.DATE);
if (unit.equals("Y")) {
cCalendar.set(mYears, cMonths, cDays);
if (cCalendar.before(mCalendar)) {
mCalendar.set(mYears + 1, cMonths, cDays);
returnDate = mCalendar.getTime();
} else {
returnDate = cCalendar.getTime();
}
}
if (unit.equals("M")) {
cCalendar.set(mYears, mMonths, cDays);
if (cCalendar.before(mCalendar)) {
mCalendar.set(mYears, mMonths + 1, cDays);
returnDate = mCalendar.getTime();
} else {
returnDate = cCalendar.getTime();
}
}
if (unit.equals("D")) {
returnDate = mCalendar.getTime();
}
} else {
returnDate = mCalendar.getTime();
}
return returnDate;
}
public static void main(String[] args) {
FDate fd = new FDate();
Date f = fd.getDate("2000-02-29");
Date f2 = fd.getDate("1999-12-31");
Date a = FDateM.calDate(f, 1, "M", f2);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String ds = df.format(a);
System.out.print(ds);
}
}