这是一个Java日期转换的工具类,可以用于任意项目。
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateUtil {
/*
* 获取当前时间 Calendar
*/
public static Calendar getCalendar() {
Calendar calendar = Calendar.getInstance();
return calendar;
}
/*
* 获取当前时间 Date
*/
public static Date getDate() {
return getCalendar().getTime();
}
/*
* 获取当前 年
*/
public static int getYear() {
return getCalendar().get(Calendar.YEAR);
}
/*
* 获取当前 月
*/
public static int getMonth() {
return getCalendar().get(Calendar.MONTH);
}
/*
* 获取当前 日
*/
public static int getDay() {
return getCalendar().get(Calendar.DAY_OF_MONTH);
}
/*
* 获取当前 时
*/
public static int getHour() {
return getCalendar().get(Calendar.HOUR_OF_DAY);
}
/*
* 获取当前 分
*/
public static int getMinute() {
return getCalendar().get(Calendar.MINUTE);
}
/*
* 获取当前 秒
*/
public static int getSecond() {
return getCalendar().get(Calendar.SECOND);
}
/*
* Date To Calendar
*/
public static Calendar dateToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/*
* Calendar To Date
*/
public static Date CalendarToDate(Calendar calendar) {
return calendar.getTime();
}
/*
* String To Date
*/
public static Date StringToDateWithHour(String dateStr, boolean withHour) {
String pattern = null;
if(withHour==true) { //dateStr must like 2012-08-02 23:59:59
pattern = "yyyy-MM-dd hh:mm:ss";
}
else { //dateStr must like 2012-08-02
pattern="yyyy-MM-dd";
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = dateFormat.parse(dateStr);
} catch(Exception e){
e.printStackTrace();
}
return date;
}
/*
* String To Calendar
*/
public static Calendar StringToCalendarWithHour(String dateStr, boolean withHour) {
String pattern = null;
if(withHour==true) { //dateStr must like 2012-08-02 23:59:59
pattern = "yyyy-MM-dd hh:mm:ss";
}
else { //dateStr must like 2012-08-02
pattern="yyyy-MM-dd";
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = dateFormat.parse(dateStr);
}
catch (Exception e) {
e.printStackTrace();
}
Calendar calendar = getCalendar();
calendar.setTime(date);
return calendar;
}
/*
* 对比两个Calendar
* 2013-01-01 compare 2015-01-01 return <1
* 2015-01-01 compare 2013-01-01 return >1
*/
public int compareCalendar(Calendar calendar1, Calendar calendar2) {
return calendar1.compareTo(calendar2);
}
}
这是一个Java数据类型转换类,可以用于任意项目。
import java.math.BigDecimal;
public class DataTypeUtil {
/*
* String To int
* if String is invalid, return 0
*/
public static int StringToIntCaseZero(String str) {
if(str==null || str.equals("")) {
return 0;
}
return Integer.parseInt(str);
}
/*
* String To Integer
* if String is invalid, return 0
*/
public static Integer StringToIntegerCaseZero(String str) {
if(str==null || str.equals("")) {
return new Integer(0);
}
return Integer.valueOf(str);
}
/*
* String To Integer
* if String is invalid, return null
*/
public static Integer StringToIntegerCaseNull(String str) {
if(str==null || str.equals("")) {
return null;
}
return Integer.valueOf(str);
}
/*
* String To double
* if String is invalid, return 0
*/
public static double StringTodouble(String str) {
if(str==null || str.equals("")) {
return 0;
}
return Double.parseDouble(str);
}
/*
* String To Double
* if String is invalid, return 0
*/
public static Double StringToDoubleCaseZero(String str) {
if(str==null || str.equals("")) {
return new Double(0);
}
return Double.valueOf(str);
}
/*
* String To Integer
* if String is invalid, return null
*/
public static Double StringToDoubleCaseNull(String str) {
if(str==null || str.equals("")) {
return null;
}
return Double.valueOf(str);
}
/*
* String To float
* if String is invalid, return 0
*/
public static float StringTofloatCaseZero(String str) {
if(str==null || str.equals("")) {
return 0;
}
return Float.parseFloat(str);
}
/*
* String To Float
* if String is invalid, return 0
*/
public static Float StringToFloatCaseZero(String str) {
if(str==null || str.equals("")) {
return new Float(0);
}
return Float.valueOf(str);
}
/*
* String To Integer
* if String is invalid, return null
*/
public static Float StringToFloatCaseNull(String str) {
if(str==null || str.equals("")) {
return null;
}
return Float.valueOf(str);
}
public static String intToString(int i) {
return String.valueOf(i);
}
public static String doubleToString(double d) {
return String.valueOf(d);
}
public static String floatToString(float f) {
return String.valueOf(f);
}
public static int decimalToint(BigDecimal decimal) {
return decimal.intValue();
}
public static Integer decimalToInteger(BigDecimal decimal) {
return new Integer(decimal.intValue());
}
public static double decimalTodouble(BigDecimal decimal) {
return decimal.doubleValue();
}
public static Double decimalToDouble(BigDecimal decimal) {
return new Double(decimal.doubleValue());
}
public static float decimalTofloat(BigDecimal decimal) {
return decimal.floatValue();
}
public static Float decimalToFloat(BigDecimal decimal) {
return new Float(decimal.floatValue());
}
}
反射
package com;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.model.Company;
import com.model.Manager;
public class Main {
public static void main(String[] args) throws Exception {
Main m = new Main();
//m.fetchClass();
//m.fetchMethod();
m.fetchContructor();
//m.fetchField();
//m.createArrayByReflect();
//m.methodParaIsArray();
}
//获取系统类
public void fetchClass() throws Exception {
//方法1
Class<?> systemClass = Class.forName("java.lang.String"); //JDK的类
Class<?> customClass = Class.forName("com.model.Company"); //自定义的类
//方法2
Class<?> companyClass = Company.class;
}
//创建对象,并调用方法
public void fetchMethod() throws Exception {
Class<?> customClass = Class.forName("com.model.Company"); //获取class
Object object = customClass.newInstance(); //调用无参数的构造函数创建对象
Class<?>[] paraClassArray = {int.class}; //方法的参数类型列表
Method method = customClass.getDeclaredMethod("displayWithPara", paraClassArray); //获取方法
method.invoke(object, new Object[]{7}); //调用方法
Method[] methods = customClass.getMethods(); //getMethods()获该类所有的public方法,包括从父类继承的public方法
Method[] declaredMethods = customClass.getDeclaredMethods(); //getDeclaredMethods()获该类所有的public和private方法,包括从父类继承的public方法
}
//获得构造方法
public void fetchContructor() throws Exception {
Class<?> customClass = Company.class;
Constructor<?> emptyConstructor = customClass.getDeclaredConstructor(new Class<?>[]{}); //获取空的构造函数
Object emptyObject = emptyConstructor.newInstance(new Object[]{});//使用带参数的构造函数创建对象
Constructor<?> customConstructor = customClass.getDeclaredConstructor(new Class<?>[]{String.class, int.class}); //获取带参数的构造函数
Object customObject = customConstructor.newInstance(new Object[]{"XXXIT", 3});
//调用方法
Method method = customClass.getDeclaredMethod("display"); //方法没参数列表时可以不写,可变参数嘛
method.invoke(emptyObject);
method.invoke(customObject);
}
//获取属性
public void fetchField() throws Exception {
Class<?> customClass = Class.forName("com.model.Company");
Field field1 = customClass.getDeclaredField("manager"); //获取属性
field1.getType().getName(); //属性的类型
Company company = new Company("XXIT", 12);
Field field2 = customClass.getDeclaredField("employeeNumber"); //获取属性
field2.setAccessible(true); //由于属性是私有的,因此需要开放访问权限
field2.getInt(company); //获取company对象的employeeNumber属性的值
Field field3 = customClass.getDeclaredField("manager");
field3.setAccessible(true);
Manager managerFieldValue = (Manager)field3.get(company);
System.out.println(managerFieldValue.getAge());
}
//反射创建数组
public void createArrayByReflect() {
Class<?> classType = String.class;
Object array = Array.newInstance(classType, 10);// 创建一个长度为10的字符串数组
Array.set(array, 3, "lyn");// 把索引位置=3的元素设为"lyn"
String str = (String) Array.get(array, 3);//获得索引位置=3的元素的值
System.out.println(str);
}
//调用参数是数组的方法
public void methodParaIsArray() throws Exception {
Class<?> classType = Company.class;
Method method = classType.getDeclaredMethod("displayWithArray", String[].class);
String[] names = new String[]{"A", "B", "C"};
method.invoke(new Company(), new Object[]{names});
}
}