import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
public class Utility {
?
?static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
?static DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
?static Calendar currentCalendar = Calendar.getInstance();
?static DecimalFormat decimalFormat = new DecimalFormat("#.00");
?
?public static Date currentDateTime() {
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??
??return currentCalendar.getTime();
?}
?
?public static String dateToString(Date date,boolean withTime) {
??
??if(date == null){
???currentCalendar.setTimeInMillis(System.currentTimeMillis());
???date = currentCalendar.getTime();
??}??
??
??return withTime ? dateTimeFormat.format(date) :dateFormat.format(date);
?}?
?public static Date stringToDate(String sdate){
??
??try {
???return isNullOrEmpty(sdate) ? currentDateTime() : dateFormat.parse(sdate);
??} catch (ParseException e) {
???return currentDateTime();
??}
?}?
?
?public static boolean before(String d1,String d2){
??return stringToDate(d1).before(stringToDate(d2));
?}
?
?public static boolean after(String d1,String d2){
??return stringToDate(d1).after(stringToDate(d2));
?}
?
?public static String getYearBegin(String year){
??try {
???if(!isNullOrEmpty(year))
????currentCalendar.set(Calendar.YEAR, Integer.parseInt(year));
???else
????currentCalendar.setTimeInMillis(System.currentTimeMillis());?
??} catch (NumberFormatException e) {
???currentCalendar.setTimeInMillis(System.currentTimeMillis());??
??}
??currentCalendar.set(Calendar.MONTH, 0);
??currentCalendar.set(Calendar.DATE, 1);
??return dateToString(currentCalendar.getTime(),false);??
?}
?
?public static String getYearEnd(String year){
??try {
???if(!isNullOrEmpty(year))
????currentCalendar.set(Calendar.YEAR, Integer.parseInt(year));
???else
????currentCalendar.setTimeInMillis(System.currentTimeMillis());?
??} catch (NumberFormatException e) {
???currentCalendar.setTimeInMillis(System.currentTimeMillis());??
??}
??currentCalendar.set(Calendar.MONTH, 11);
??currentCalendar.set(Calendar.DATE, 31);
??return dateToString(currentCalendar.getTime(),false);??
?}
?
?public static String getYearBegin(){
??return getYearBegin(null);??
?}
?
?public static String getYearEnd(){
??return getYearEnd(null);??
?}
?
?public static String currentDateString(){
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??
??return dateFormat.format(currentCalendar.getTime());
?}
?
?public static String currentDateTimeToString() {
??????? currentCalendar.setTimeInMillis(System.currentTimeMillis());
???????
??????? return dateTimeFormat.format(currentCalendar.getTime());
?}
?
?public static String getDateFlag(boolean withMonth) {
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??
??return getDateFlag(currentCalendar.getTime(),withMonth);
?}
?
?public static String getDateFlag(String dateString){
??
??return (dateString!=null||!"".equals(dateString)) ? dateString.replaceAll("-", "").substring(0,6) : getDateFlag(true);
?}
?
?public static String getDateFlag(String dateString,boolean withMonth){
??
??return (dateString!=null||!"".equals(dateString)) ? dateString.replaceAll("-", "").substring(0, withMonth ? 6 : 4) + (withMonth ? "":"00") : getDateFlag(withMonth);
?}
?
?public static String getDateFlag(Date date){
??
??return getDateFlag(date,true);
?}
?
?public static String getDateFlag(Date date,boolean withMonth){
??
??return (date!=null)? dateFormat.format(date).replaceAll("-", "").substring(0, withMonth ? 6 : 4) + (withMonth ? "":"00") : getDateFlag(withMonth);
?}
?
?@SuppressWarnings("unchecked")
?public static boolean isNullOrEmpty(Collection c){
??return (c == null||c.size()==0);
?}
?
?public static boolean isNullOrEmpty(String s){
??return (s == null||s.length()==0);
?}
?
?public static String getCurrentYear(){
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??
??return currentCalendar.get(Calendar.YEAR)+"00";
?}
?
?public static String getNextYear(String yearFlag){
??
??int year = Integer.parseInt(yearFlag.substring(0, 4));
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??currentCalendar.set(Calendar.YEAR, year+1);
??
??return currentCalendar.get(Calendar.YEAR)+"00";
?}
?
?public static String getNextMonthDateFlag(){
??
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??currentCalendar.add(Calendar.MONTH, 1);
??
??return getDateFlag(currentCalendar.getTime(),true);
?}
?
?public static Integer getCurrentMonth(){
??currentCalendar.setTimeInMillis(System.currentTimeMillis());
??
??return currentCalendar.get(Calendar.MONTH)+1;
?}
?
?public static String format(double number) throws ParseException{
??return decimalFormat.format(number);
?}
?
?public static Number parse(String source) throws ParseException{
??return decimalFormat.parse(source);
?}
?
?public static String changeMonth(int i){
??String month = "";
??if(i<10){
???month = "0"+i;
??}else {
???month = String.valueOf(i);
??}
??return month;
?}
?
?public static BigDecimal toBigDecimal(String value){
??try {
???return BigDecimal.valueOf(Double.valueOf(value).doubleValue());
??} catch (Exception e) {
???return new BigDecimal(0);
??}
?}
}