⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateutils.java

📁 j2ee源码
💻 JAVA
字号:
/*
 * Created on 2005/11/1
 */
package com.leeman.common.util;

import java.util.*;
import java.text.*;

/**
 * @author Dennis
 */
public class DateUtils {
	
	public static int getCurrentMonth()
	{	
		Calendar gc = new GregorianCalendar();
		gc.getTime();
		return gc.get(GregorianCalendar.MONTH)+1;
	}

	public static int getHourOfDay(Date date)
	{
		GregorianCalendar gc = DateUtils.getGregorianCalendarInstance(date);
		return gc.get(GregorianCalendar.HOUR_OF_DAY);
	}

	public static String getCurrentMonthShortFormat() throws Exception
	{
		return monthToShortFormat(DateUtils.getCurrentMonth());
	}

	public static String monthToShortFormat(int month) throws Exception
	{
		String shortMonth = "";
		switch (month)
		{
			case 1:
				shortMonth = "01";
				break;
			case 2:
				shortMonth = "02";
				break;
			case 3:
				shortMonth = "03";
				break;
			case 4:
				shortMonth = "04";
				break;
			case 5:
				shortMonth = "05";
				break;
			case 6:
				shortMonth = "06";
				break;
			case 7:
				shortMonth = "07";
				break;	
			case 8:
				shortMonth = "08";
				break;
			case 9:
				shortMonth = "09";
				break;
			case 10:
				shortMonth = "10";
				break;
			case 11:
				shortMonth = "11";
				break;
			case 12:
				shortMonth = "12";
				break;
			default:
				throw new Exception ("Month not Valid");
		}
		return shortMonth;
	}

	public static int getCurrentYear()
	{	
		Calendar gc = new GregorianCalendar();
		gc.getTime();
		return gc.get(GregorianCalendar.YEAR);
	}
			
	public static long dateDiff(java.util.Date date1, java.util.Date date2)
		{
			Calendar cal1 = null; 
			Calendar cal2 = null;
   
			cal1=Calendar.getInstance(); 
			cal2=Calendar.getInstance();
	
			cal1.setTime(date1);          
			long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);
    
			cal2.setTime(date2);
			long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);
	
			//Use integer calculation, truncate the decimals
			int hr1   = (int)(ldate1/3600000); //60*60*1000
			int hr2   = (int)(ldate2/3600000);

			int days1 = (int)hr1/24;
			int days2 = (int)hr2/24;

			int dateDiff  = days2 - days1;

			return dateDiff;
		}	

	/**
	 * Get the Date, if the day specified is greater than the valid value, maximun value of the year-month will be return
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 * @throws Exception
	 */
	public static GregorianCalendar getValidDateOfMonth(int year, int month, int day) throws Exception
	{
		if (day > 31)
		{
			throw new Exception ("Invalid Day in getValidDateOfMonth.");
		}

		int cDay = 0;
		int daysInMonth = 0;
	 
		if ((month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10) || (month==12))
		{
			daysInMonth = 31;
		} 
		else if ((month==4) || (month==6) || (month==9) || (month==11))
		{
			daysInMonth = 30; 
		}
		else if(month ==2)
		{
			daysInMonth = daysInFebruary(year);
		}
		else
		{
			throw new Exception ("Invalid Month in getValidDateOfMonth.");
		}

		if (day > daysInMonth)
		{
			cDay = daysInMonth;
		}
		else
		{
			cDay = day;
		}

		GregorianCalendar gc = new GregorianCalendar();
		gc.set(year, month -1, cDay, 0, 0, 0); 
		gc.isLeapYear(0);

		return gc;
	}

	private static int daysInFebruary (int year)
	{
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
		
	public static java.util.GregorianCalendar getGregorianCalendarInstance(java.util.Date date)
		{
			java.util.GregorianCalendar c = new java.util.GregorianCalendar();
			c.setTime(date);
			return c;
		}
		
	/**
	 * Get Current Date without time (ie.00:00:00)
	 * @return Date
	 * @throws ParseException
	 */
	public static Date getCurrentDate() throws ParseException
	{
		String tmpDateFormat="dd-MM-yyyy";		
		GregorianCalendar gc = (GregorianCalendar)GregorianCalendar.getInstance();
		java.text.SimpleDateFormat sdf = 
						  new java.text.SimpleDateFormat(tmpDateFormat);
		return sdf.parse(sdf.format(gc.getTime()));
	}
	
	/**
	 * Get Date before or after Current Date without time (ie.00:00:00)
	 * @param field
	 * @param amount 
	 * @return Date
	 * @throws ParseException
	 */
	public static Date getCurrentDateAdd(int field, int amount ) throws ParseException
	{
		String tmpDateFormat="dd-MM-yyyy";		
		GregorianCalendar gc = (GregorianCalendar)GregorianCalendar.getInstance();
		gc.add(field, amount);
		java.text.SimpleDateFormat sdf = 
						  new java.text.SimpleDateFormat(tmpDateFormat);
		return sdf.parse(sdf.format(gc.getTime()));
	}	

	/**
	 * Get Current Date Time 
	 * @return Date
	 */
	public static Date getCurrentDateTime()
	{
		GregorianCalendar gc = (GregorianCalendar)GregorianCalendar.getInstance();
		return gc.getTime();
	}
	
	public static Date getCurrentDateTimeAdd(int field, int amount ) throws ParseException
		{
			String tmpDateFormat="dd/MM/yyyy HH:mm:ss";		
			GregorianCalendar gc = (GregorianCalendar)GregorianCalendar.getInstance();
			gc.add(field, amount);
			java.text.SimpleDateFormat sdf = 
							  new java.text.SimpleDateFormat(tmpDateFormat);
			return sdf.parse(sdf.format(gc.getTime()));
		}	
	
	//The Following function is create for calculate of est weight and est rolls
	public static Double wtKg_to_Tons(Double WtKg)
	{
		if (WtKg != null)
			return new Double(wtKg_to_Tons(WtKg.doubleValue()));
		else
			return null; 
	}

	public static double wtKg_to_Tons(double WtKg)
	{
		return WtKg / 1000;
	}

	public static Double wtTons_to_Kg(Double WtTons)
	{
		if (WtTons != null)
			return new Double(wtTons_to_Kg(WtTons.doubleValue()));
		else
			return null;
	}

	public static double wtTons_to_Kg(double WtTons)
	{
		return WtTons * 1000;
	}

	public static double uomIN_to_MM(double uomIN)
	{
		return uomIN * 25.4;
	}

	public static double uomMM_to_M(double uomMM)
	{
		return uomMM / 1000;
	}

	public static Double round(Double data, int dp) throws Exception
	{
		return new Double(round(data.doubleValue(), dp));
	}

	public static double round(double data, int dp) throws Exception
	{ 
		if (dp < 0 || dp > 8){
			throw new Exception ("Unsupported rounding decimal place.");
		}
		else{
			double DP[] = new double[9];
			DP[0] = 1;
			DP[1] = 10;
			DP[2] = 100;
			DP[3] = 1000;
			DP[4] = 10000;
			DP[5] = 100000;
			DP[6] = 1000000;
			DP[7] = 10000000;
			DP[8] = 100000000;
		
			return Math.round(data * DP[dp]) / DP[dp];
		}
	}

	public static double calc_est_WeightTons(Double paperSize, String UOM, Double paperTypeLength, Double baseWeight, Long Qty) throws Exception
	{
		if (paperSize == null || UOM == null || paperTypeLength == null || baseWeight == null){
			throw new Exception ("est_WeightTons(): invalid paper size/uom/paper type length/ base weight");	
		}
		else if (!UOM.equals("IN") && !UOM.equals("MM")){
			throw new Exception ("est_WeightTons(): uom should be in MM or IN");			
		}
		else{
			double paperSizeInMetre = 0;
			if ( UOM.equalsIgnoreCase("MM"))
			{
				paperSizeInMetre = uomMM_to_M(paperSize.doubleValue());
			}
			else if(UOM.equalsIgnoreCase("IN"))
			{
				paperSizeInMetre = uomMM_to_M(uomIN_to_MM(paperSize.doubleValue()));
			}
		
			double estWtTonsUnit = wtKg_to_Tons(paperSizeInMetre * paperTypeLength.doubleValue() * baseWeight.doubleValue());
		
			double estWtTons = round(estWtTonsUnit, 4) * Qty.doubleValue();
		
			return round((estWtTons),4);
		}
	}

	public static long calc_est_Rolls(Double paperSize, String UOM, Double paperTypeLength, Double baseWeight, Double Est_weight) throws Exception
	{
		if (paperSize == null || UOM == null || paperTypeLength == null || baseWeight == null){
			throw new Exception ("est_Rolls(): invalid paper size/uom/paper type length/ base weight");	
		}
		else if (!UOM.equals("IN") && !UOM.equals("MM")){
			throw new Exception ("est_Rolls(): uom should be in MM or IN");			
		}
		else{
			double paperSizeInMetre = 0;
			if ( UOM.equalsIgnoreCase("MM"))
			{
				paperSizeInMetre = uomMM_to_M(paperSize.doubleValue());
			}
			else if(UOM.equalsIgnoreCase("IN"))
			{
				paperSizeInMetre = uomMM_to_M(uomIN_to_MM(paperSize.doubleValue()));
			}

			double estWtTonsUnit = wtKg_to_Tons(paperSizeInMetre * paperTypeLength.doubleValue() * baseWeight.doubleValue());

			Long estRolls = new Long ( new Double(round(Est_weight.doubleValue() / round(estWtTonsUnit, 4),0)).longValue() ) ;

			return estRolls.longValue() ;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -