Skip to content

Commit

Permalink
Added new middleware date methods as dependencies for DateUtil class …
Browse files Browse the repository at this point in the history
…in commons

BMS-2015

Reviewer: Aldrin Batac
  • Loading branch information
cvpcasada committed Dec 3, 2015
1 parent 9e5ae7f commit 38e7432
Showing 1 changed file with 106 additions and 21 deletions.
127 changes: 106 additions & 21 deletions src/main/java/org/generationcp/middleware/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
/*******************************************************************************
* Copyright (c) 2012, All Rights Reserved.
*
*
* Generation Challenge Programme (GCP)
*
*
*
*
* This software is licensed for use under the terms of the GNU General Public
* License (http://bit.ly/8Ztv8M) and the provisions of Part F of the Generation
* Challenge Programme Amended Consortium Agreement (http://bit.ly/KQX1nL)
*
*
*******************************************************************************/
/*******************************************************************************
* Copyright (c) 2012, All Rights Reserved.
*
*
* Generation Challenge Programme (GCP)
*
*
*
*
* This software is licensed for use under the terms of the GNU General Public
* License (http://bit.ly/8Ztv8M) and the provisions of Part F of the Generation
* Challenge Programme Amended Consortium Agreement (http://bit.ly/KQX1nL)
*
*
*******************************************************************************/
package org.generationcp.middleware.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* A utility class used to get primitive values of wrapper classes, check for
* null values, and list functions such as getting the max, existence of a
* value, existence of null, and making a list read only.
*
*
*/
public class Util{

public static final String DATE_AS_NUMBER_FORMAT = "yyyyMMdd";
public static final String DATE_AS_NUMBER_FORMAT = "yyyyMMdd";
public static final String FRONTEND_DATE_FORMAT = "yyyy-MM-dd";
// NOTE: Future Improvement: BMS should only use one front end date format
public static final String FRONTEND_DATE_FORMAT_2 = "MM/dd/yyyy";
public static final String FRONTEND_TIMESTAMP_FORMAT = "yyyy-MM-dd hh:mm:ss";


/**
* Get the boolean value of <code>value</code>.
*
*
* @param value
* @return the boolean value of <code>value</code>. If <code>value</code> is
* null, this method returns false.
Expand All @@ -51,7 +58,7 @@ public static boolean getValue(Boolean value, boolean defaultValue) {

/**
* Test whether <code>obj</code> is equal to one of the specified objects.
*
*
* @param obj
* @param objs
* @return true if the obj is one of the objects
Expand Down Expand Up @@ -86,7 +93,7 @@ public static boolean isAllNull(Object... args) {

/**
* Test whether <code>value</code> is equal to all of the specified values.
*
*
* @param value
* @param values
* @return true if value is equal to all values.
Expand All @@ -108,7 +115,7 @@ public static boolean isAllEqualTo(Double value, Double... values) {
/**
* Test whether the specified list is "empty". A <code>null</code> value is
* considered "empty".
*
*
* @param list
* @return true if the given list is empty.
*/
Expand Down Expand Up @@ -146,13 +153,91 @@ public static <T> List<T> makeReadOnlyList(T... objects) {

return Arrays.asList(objects);
}

public static Integer getCurrentDate(){
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(DATE_AS_NUMBER_FORMAT);
String dateNowStr = formatter.format(now.getTime());
Integer dateNowInt = Integer.valueOf(dateNowStr);
return dateNowInt;

/**
* Returns the current date in format "yyyyMMdd" as Integer
*
* @return current date as Integer
*/
public static Integer getCurrentDateAsIntegerValue() {
return Integer.valueOf(Util.getCurrentDateAsStringValue());
}

/**
* Returns the current date in format "yyyyMMdd" as Long
*
* @return current date as Long
*/
public static Long getCurrentDateAsLongValue() {
return Long.valueOf(Util.getCurrentDateAsStringValue());
}

/**
* Returns the current date in format "yyyyMMdd" as String
*
* @return current date as String
*/
public static String getCurrentDateAsStringValue() {
return Util.getSimpleDateFormat(Util.DATE_AS_NUMBER_FORMAT).format(Util.getCurrentDate().getTime());
}

/**
* Returns the current date
*
* @return current date as Date
*/
public static Date getCurrentDate() {
return Util.getCalendarInstance().getTime();
}

/**
* Returns the calendar instance
*
* @return calendar instance
*/
public static Calendar getCalendarInstance() {
Locale currentLocale = Locale.getDefault(Locale.Category.DISPLAY);
return Calendar.getInstance(currentLocale);
}

/**
* Returns the current date in the specified format as String
*
* @return current date as String
*/
public static String getCurrentDateAsStringValue(String format) {
return Util.getSimpleDateFormat(format).format(Util.getCurrentDate().getTime());
}

/**
* Returns the SimpleDateFormat of the current display locale
*
* @return SimpleDateFormat
*/
public static SimpleDateFormat getSimpleDateFormat(String format) {
Locale currentLocale = Locale.getDefault(Locale.Category.DISPLAY);
SimpleDateFormat formatter = new SimpleDateFormat(format, currentLocale);
formatter.setLenient(false);
return formatter;
}

/**
* Returns the date in the specified format as String
*
* @return date in the specified format as String
*/
public static String formatDateAsStringValue(Date date, String format) {
return Util.getSimpleDateFormat(format).format(date.getTime());
}

/**
* Returns the date object from the specified format
*
* @return date object
* @throws ParseException
*/
public static Date parseDate(String date, String format) throws ParseException {
SimpleDateFormat formatter = Util.getSimpleDateFormat(format);
return formatter.parse(date);
}
}

0 comments on commit 38e7432

Please # to comment.