Sunteți pe pagina 1din 10

package dateUtilities;

import java.util.*;
import java.text.*;
import java.lang.*;
import java.util.Date;
import java.text.SimpleDateFormat;

/* note: format for date is case sensitive MM-dd-yyyy */

public class DateUtil {

public static int DIFF_YEAR=0;


public static int DIFF_WEEK=1;
public static int DIFF_DAY=2;
public static int DIFF_HOUR=3;
public static int DIFF_MINUTE=4;
public static int DIFF_SECOND=5;
public static int DIFF_MONTH=6;

private static long SECOND = 1000;


private static long MINUTE = 60 * SECOND;
private static long HOUR = MINUTE * 60;
private static long DAY = HOUR * 24;
private static long WEEK = DAY * 7;
private static long MONTH = DAY * 30;
private static long YEAR = DAY * 365;

private static long ATLANTIC_STANDARD_GMT_OFFSET = -4 * HOUR; //let's use


absolute value
private static long PACIFIC_STANDARD_GMT_OFFSET = -8 * HOUR; //actuals are
negitive
private static long MOUNTAIN_STANDARD_GMT_OFFSET = -7 * HOUR;
private static long CENTRAL_STANDARD_GMT_OFFSET = -6 * HOUR;
private static long EASTERN_STANDARD_GMT_OFFSET = -5 * HOUR;

private static long ATLANTIC_DAYLIGHT_GMT_OFFSET = -3 * HOUR; //let's use


absolute value
private static long PACIFIC_DAYLIGHT_GMT_OFFSET = -7 * HOUR; //actuals are
negitive
private static long MOUNTAIN_DAYLIGHT_GMT_OFFSET = -6 * HOUR;
private static long CENTRAL_DAYLIGHT_GMT_OFFSET = -5 * HOUR;
private static long EASTERN_DAYLIGHT_GMT_OFFSET = -4 * HOUR;

private static String ATLANTIC_STANDARD = "AS";


private static String PACIFIC_STANDARD = "PS";
private static String MOUNTAIN_STANDARD = "MS";
private static String CENTRAL_STANDARD = "CS";
private static String EASTERN_STANDARD = "ES";

private static String ATLANTIC_DAYLIGHT = "AD";


private static String PACIFIC_DAYLIGHT = "PD";
private static String MOUNTAIN_DAYLIGHT = "MD";
private static String CENTRAL_DAYLIGHT = "CD";
private static String EASTERN_DAYLIGHT = "ED";

private Date baseDate=null;


private GregorianCalendar calendar;
private SimpleTimeZone timeZone;

/* default constructor set Date to current date */


public DateUtil() {
baseDate = new Date();
init();
}

public DateUtil(Date date) {


baseDate = date;
init();
}

public DateUtil(String date, String format) {


setDate(date, format);
init();
}

private void init() {


this.calendar = new GregorianCalendar();
}

public GregorianCalendar getCalendar() {


return(this.calendar);
}

public void setDate(Date date) {


try {
baseDate = date;
} catch (Exception e) {
baseDate = new Date();
}
}

//Format : MM/DD/YYYY || MM-DD-YYYY


public void setDate(String date, String format) {
SimpleDateFormat sdfInput = new SimpleDateFormat(format);
try {
setDate(sdfInput.parse(date));
} catch(Exception e) {
e.printStackTrace();
setDate(new Date());
}
}

public Date getDate() {


return(baseDate);
}

//Format : MM/DD/YYYY || MM-DD-YYYY HH:MM:SS


public String getDate(String format) {
SimpleDateFormat sdfOutput = new SimpleDateFormat (format);
return(sdfOutput.format( baseDate ));
}
public int getDayOfWeek() {

calendar.clear();
calendar.setTime(this.getDate());
return(calendar.get(calendar.DAY_OF_WEEK));

public int getDayOfMonth() {

calendar.clear();
calendar.setTime(this.getDate());
return(calendar.get(calendar.DAY_OF_MONTH));

public int getDayOfYear() {


calendar.clear();
calendar.setTime(this.getDate());
return(calendar.get(calendar.DAY_OF_YEAR));

public void addMilliSeconds(int milliSeconds) {

calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.MILLISECOND, milliSeconds);
this.getDate().setTime(calendar.getTime().getTime());
}

public void addSeconds(int seconds) {


//this.getDate().setTime(this.getDate().getTime() + (days *
this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.SECOND, seconds);
this.getDate().setTime(calendar.getTime().getTime());
}

public void addMinutes(int minutes) {


//this.getDate().setTime(this.getDate().getTime() + (days *
this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.MINUTE, minutes);
this.getDate().setTime(calendar.getTime().getTime());
}

public void addHours(int hours) {


//this.getDate().setTime(this.getDate().getTime() + (days *
this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.HOUR, hours);
this.getDate().setTime(calendar.getTime().getTime());
}
public void addDays(int days) {
//this.getDate().setTime(this.getDate().getTime() + (days * this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.DAY_OF_YEAR, days);
this.getDate().setTime(calendar.getTime().getTime());
}

public void addWeeks(int weeks) {


//this.getDate().setTime(this.getDate().getTime() + (days * this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.WEEK_OF_YEAR, weeks);
this.getDate().setTime(calendar.getTime().getTime());
}

public void addMonths(int months) {


//this.getDate().setTime(this.getDate().getTime() + (days * this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.MONTH, months);
this.getDate().setTime(calendar.getTime().getTime());
}
public void addYears(int years) {
//this.getDate().setTime(this.getDate().getTime() + (days *
this.DAY));
calendar.clear();
calendar.setTime(this.getDate());
calendar.add(Calendar.YEAR, years);
this.getDate().setTime(calendar.getTime().getTime());
}

public long getDifference(Date date, int magnitute) {


long difference=0;
switch (magnitute) {
case 0:

try {
difference = (date.getTime() - this.getDate().getTime()) /
this.YEAR;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}

break;

case 1:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.WEEK;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}

break;
case 2:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.DAY;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}

break;
case 3:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.HOUR;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}
break;
case 4:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.MINUTE;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}
break;
case 5:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.SECOND;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}

break;

case 6:
try {
difference = (date.getTime() - this.getDate().getTime()) /
this.MONTH;
} catch(Exception e) {
e.printStackTrace();
difference = 0;
}

break;
default:
difference = 0;
break;
}

return(difference);
}

public long getDifference(String date, String format, int magnitute) {


Date inptDate;
SimpleDateFormat sdfInput = new SimpleDateFormat(format);

try {
inptDate = sdfInput.parse(date);
} catch(Exception e) {
e.printStackTrace();
inptDate = new Date();
}

return(getDifference(inptDate, magnitute));
}

//If it is a leap year there is an extra day in febuary

public boolean isLeapYear (int year) {


return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}

public static void main(String[] args) {


long yearDiff=0,
weekDiff=0,
dayDiff=0,
hourDiff=0,
minuteDiff=0,
secondDiff=0;

if(args.length ==3) {

DateUtil dateUtil = new DateUtil(args[1],args[0]);

yearDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_YEAR);
weekDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_WEEK);
dayDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_DAY);
hourDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_HOUR);
minuteDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_MINUTE);
secondDiff =
dateUtil.getDifference(args[2],args[0],dateUtil.DIFF_SECOND);

//print difference report


System.out.println("The different report:\n");
System.out.println("Year(s) : " + yearDiff);
System.out.println("Week(s) : " + weekDiff);
System.out.println("Day(s) : " + dayDiff);
System.out.println("Hour(s) : " + hourDiff);
System.out.println("Minute(s) : " + minuteDiff);
System.out.println("Second(s) : " + secondDiff);

System.out.println("\n\nCurrent date: " + dateUtil.getDate("MM-


dd-yyyy"));

//date addition
System.out.println("");
dateUtil.addDays(1);
System.out.println("\ndate addition plus 1 day:\n");
System.out.println("New date: " + dateUtil.getDate("MM-dd-
yyyy"));

//date addition
dateUtil.addDays(31);
System.out.println("\ndate addition plus 31 more days:\n");
System.out.println("New date: " + dateUtil.getDate("MM-dd-
yyyy"));

//date addition
dateUtil.addMonths(1);
System.out.println("\ndate addition plus 1 month:\n");
System.out.println("New date: " + dateUtil.getDate("MM-dd-
yyyy"));

//date addition
dateUtil.addMonths(31);
System.out.println("\ndate addition plus 31 month:\n");
System.out.println("New date: " + dateUtil.getDate("MM-dd-
yyyy"));

dateUtil.addWeeks(2);
System.out.println("\ndate addition plus 2 weeks:\n");
System.out.println("New date: " + dateUtil.getDate("MM-dd-
yyyy"));

//calendar defaults report leapyear


//System.out.println("2002 is Leap Year: " +
String.valueOf(dateUtil.getCalendar().isLeapYear(2002)));
//System.out.println("2003 is Leap Year: " +
String.valueOf(dateUtil.getCalendar().isLeapYear(2003)));
//System.out.println("2004 is Leap Year: " +
String.valueOf(dateUtil.getCalendar().isLeapYear(2004)));
//System.out.println("2005 is Leap Year: " +
String.valueOf(dateUtil.getCalendar().isLeapYear(2005)));
//display default timezone
System.out.println("TimeZone: " +
dateUtil.getCalendar().getTimeZone().getDisplayName());

} else {

System.out.println("Usage: Java DateUtil MM-dd-yyyy 04-30-2002


05-22-2003" + "\nargs.length:" + args.length);

}
package aaKPIExtract.util.Date;

import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import com.wm.data.IData;
import com.wm.data.IDataCursor;
import dateUtilities.DateUtil;

public final class dateModifier_SVC

/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void dateModifier(IData pipeline) throws ServiceException
{
String baseDate, outDate, tmp, units, format;
int amount;

IDataCursor idc = pipeline.getCursor();


try {
idc.first("format");
format = (String)idc.getValue();

idc.first("baseDate");
baseDate = (String)idc.getValue();

idc.first("amount");
tmp = (String)idc.getValue();
amount = Integer.parseInt(tmp);

idc.first("units");
units = (String)idc.getValue();

DateUtil dateUtil = new DateUtil();


dateUtil.setDate(baseDate,format);
if(units.compareTo("days")== 0){
dateUtil.addDays(amount);
}
else
if(units.compareTo("hours")== 0){
dateUtil.addHours(amount);
}
else
if(units.compareTo("minutes")== 0){
dateUtil.addMinutes(amount);
}
else
if(units.compareTo("seconds")== 0){
dateUtil.addSeconds(amount);
}
else
if(units.compareTo("milliseconds")== 0){
dateUtil.addMilliSeconds(amount);
}
else
if(units.compareTo("months")== 0){
dateUtil.addMonths(amount);
}
else
if(units.compareTo("years")== 0){
dateUtil.addYears(amount);
}
outDate = dateUtil.getDate(format);

replaceInsert(idc,"outDate", outDate);
replaceInsert(idc,"status", "true");

} catch (Exception e) {
replaceInsert(idc,"status", "false");
replaceInsert(idc,"errorText", e.getMessage());
}

idc.destroy();

// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---

public static void replaceInsert(IDataCursor idc, String key, Object value)


{
if(idc.first(key))
{
idc.setValue(value);
}
else
{
idc.insertAfter(key, value);
}

// --- <<IS-END-SHARED-SOURCE-AREA>> ---

/**
* The service implementations given below are read-only and show only the
* method definitions and not the complete implementation.
*/
public static final void getDateDifference(IData pipeline)
throws ServiceException {
}
final static dateModifier_SVC _instance = new dateModifier_SVC();

static dateModifier_SVC _newInstance() { return new dateModifier_SVC(); }

static dateModifier_SVC _cast(Object o) { return (dateModifier_SVC)o; }

S-ar putea să vă placă și