package txtTest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateAndTime {
// date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException, java.text.ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}
public static String numToDate(int number,String formatType){
Date date = new Date(number);
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}
public static void main(String[] args) throws ParseException {
int year;
int month;
int day;
DateAndTime du = new DateAndTime();
//String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
String date1 = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
System.out.println(time);
System.out.println(time1);
System.out.println(date1);
//老版本
Date date = new Date();
year = date.getYear()+1900;
month = date.getMonth()+1;
day = date.getDate();
System.out.println(year+"年"+month+"月"+day+"日");
//新版本
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
//毫秒数
long now = System.currentTimeMillis();
Date date2 = new Date(now); //或 : date.setTime(now); long-->Date
System.out.println(date2.getDate());
//时间类型转换 long <-> Date <-> Calendar
calendar.setTime(date2); // Date-->Calendar
System.out.println(calendar.get(Calendar.YEAR));
calendar.getTime(); // Calendar-->Date
System.out.println(calendar.getTime().getDate());
date.getTime();
System.out.println(date.getTime());// Date-->long
//时间输入输出
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String today = fmt.format(date2); // Date-->String
try {
Date date3 = fmt.parse("2015-12-19 16:20:00"); // String-->Date
System.out.println(date3);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(today);
//利用Calendar时间计算
calendar.add(Calendar.YEAR, 2);
calendar.add(Calendar.MONTH, 1);
System.out.println(fmt.format(calendar.getTime()));
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateAndTime {
// date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException, java.text.ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}
public static String numToDate(int number,String formatType){
Date date = new Date(number);
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}
public static void main(String[] args) throws ParseException {
int year;
int month;
int day;
DateAndTime du = new DateAndTime();
//String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
String date1 = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
System.out.println(time);
System.out.println(time1);
System.out.println(date1);
//老版本
Date date = new Date();
year = date.getYear()+1900;
month = date.getMonth()+1;
day = date.getDate();
System.out.println(year+"年"+month+"月"+day+"日");
//新版本
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
//毫秒数
long now = System.currentTimeMillis();
Date date2 = new Date(now); //或 : date.setTime(now); long-->Date
System.out.println(date2.getDate());
//时间类型转换 long <-> Date <-> Calendar
calendar.setTime(date2); // Date-->Calendar
System.out.println(calendar.get(Calendar.YEAR));
calendar.getTime(); // Calendar-->Date
System.out.println(calendar.getTime().getDate());
date.getTime();
System.out.println(date.getTime());// Date-->long
//时间输入输出
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String today = fmt.format(date2); // Date-->String
try {
Date date3 = fmt.parse("2015-12-19 16:20:00"); // String-->Date
System.out.println(date3);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(today);
//利用Calendar时间计算
calendar.add(Calendar.YEAR, 2);
calendar.add(Calendar.MONTH, 1);
System.out.println(fmt.format(calendar.getTime()));
}
}
运行结果:
1350261893
1350305093
1970-01-16 11:07:50
2016年1月6日
2016年1月6日
6
2016
6
1452066821073
Sat Dec 19 16:20:00 CST 2015
2016-01-06 15:53:41
2018-02-06 15:53:41