Created
November 12, 2014 17:38
-
-
Save ashleywxwx/92abb43e0823867698e3 to your computer and use it in GitHub Desktop.
RC Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.recursivechaos.rcutils; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.Locale; | |
| import org.apache.commons.logging.Log; | |
| import org.apache.commons.logging.LogFactory; | |
| /** | |
| * RisDateUtil finds dates based on either system time, or based upon input | |
| * provided. | |
| * | |
| * @author Andrew Bell | |
| * | |
| */ | |
| public class RcDateUtil { | |
| private static Log log = LogFactory.getLog(RcDateUtil.class); | |
| private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); | |
| /** | |
| * Returns a Date object set to the beginning of the current month. | |
| * | |
| * @return Date of the start of the current month | |
| */ | |
| public static Date getBeginningOfMonth() { | |
| Date date = new Date(); | |
| return getBeginningOfMonth(date); | |
| } | |
| /** | |
| * Calculates the start of a month based on provided Date | |
| * | |
| * @param date | |
| * to find beginning of month from | |
| * @return Date of the start of the given month | |
| */ | |
| public static Date getBeginningOfMonth(Date date) { | |
| log.debug("Get beginning of month from: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(setTimeToBeginningOfDay(date)); | |
| cal.set(Calendar.DAY_OF_MONTH, 1); | |
| log.debug("First day of month: " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Returns a Date object set to the beginning of the next month | |
| * | |
| * @return date at the beginning of the next month | |
| */ | |
| public static Date getBeginningOfNextMonth() { | |
| Date date = new Date(); | |
| return getBeginningOfNextMonth(date); | |
| } | |
| /** | |
| * Returns a Date object set to the beginning of the next month, in relation | |
| * to the date provided | |
| * | |
| * @param date | |
| * to find the beginning of the next month from | |
| * @return date of the beginning of the next month. | |
| */ | |
| public static Date getBeginningOfNextMonth(Date date) { | |
| log.debug("Finding beginning of next month: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(setTimeToBeginningOfDay(date)); | |
| cal.add(Calendar.MONTH, 1); | |
| cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Returns a Date object set to the beginning of the previous month. | |
| * | |
| * @return Date at beginning of first day of the previous month. | |
| */ | |
| public static Date getBeginningOfPreviousMonth() { | |
| Date date = new Date(); | |
| return getBeginningOfPreviousMonth(date); | |
| } | |
| /** | |
| * Returns a Date object set to the beginning of the previous month | |
| * | |
| * @param date | |
| * representing the date to calculate from | |
| * @return Date at beginning of the previous month | |
| */ | |
| public static Date getBeginningOfPreviousMonth(Date date) { | |
| log.debug("Finding First day of Previous Month: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(setTimeToBeginningOfDay(date)); | |
| cal.add(Calendar.MONTH, -1); // Subtract month | |
| cal.set(Calendar.DATE, 1); // Set day to 1st | |
| log.debug("First day of month: " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Calculates the end of the current month | |
| * | |
| * @return Date at end of the current month | |
| */ | |
| public static Date getEndOfMonth() { | |
| Date date = new Date(); | |
| return getEndOfMonth(date); | |
| } | |
| /** | |
| * Calculates the end of a month based on provided Date | |
| * | |
| * @param date | |
| * to find the end of the month from | |
| * @return Date of the end of the given month | |
| */ | |
| public static Date getEndOfMonth(Date date) { | |
| log.debug("Finding end of month: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(setTimeToEndOfDay(date)); | |
| cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH)); | |
| log.debug("End of month: " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Returns what half the date falls in, using standard quarters. Returns a 0 | |
| * on error. | |
| * | |
| * @param date | |
| * Date to find half from | |
| * @return int representing which half | |
| */ | |
| public static int getHalf(Date date) { | |
| log.debug("Getting half for date: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(Locale.US); | |
| cal.setTime(date); | |
| int month = cal.get(Calendar.MONTH); | |
| int half = (month / 6) + 1; | |
| // error checking | |
| log.debug("Date is in half " + half); | |
| if ((half < 1) && (half > 2)) { | |
| half = 0; | |
| } | |
| return half; | |
| } | |
| /** | |
| * Returns a Date object set to the end of the previous month. | |
| * | |
| * @return Date at end of last day of the previous month. | |
| */ | |
| public static Date getEndOfPreviousMonth() { | |
| Date date = new Date(); | |
| return getEndOfPreviousMonth(date); | |
| } | |
| /** | |
| * Returns a Date object set to the end of the prior month given. | |
| * | |
| * @param Date | |
| * representing the date to calculate from | |
| * @return Date at the end of the previous month | |
| */ | |
| public static Date getEndOfPreviousMonth(Date date) { | |
| log.debug("Finding last day of previous month: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTime(setTimeToEndOfDay(date)); | |
| cal.add(Calendar.MONTH, -1); | |
| cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH)); | |
| log.debug("Last day of month: " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Returns what quarter the date falls in, using standard quarters. Returns | |
| * a 0 on error. | |
| * | |
| * @param date | |
| * Date to find quarter from | |
| * @return int representing which quarter | |
| */ | |
| public static int getQuarter(Date date) { | |
| log.debug("Getting quarter for date: " + sdf.format(date)); | |
| Calendar cal = Calendar.getInstance(Locale.US); | |
| cal.setTime(date); | |
| int month = cal.get(Calendar.MONTH); | |
| int quarter = (month / 3) + 1; | |
| log.debug("Date is in quarter " + quarter); | |
| // error checking | |
| if ((quarter < 1) && (quarter > 4)) { | |
| quarter = 0; | |
| } | |
| return quarter; | |
| } | |
| /** | |
| * Sets the given date's time to 00:00:00:00 | |
| * | |
| * @param date | |
| * @return date modified to beginning of day | |
| */ | |
| public static Date setTimeToBeginningOfDay(Date date) { | |
| log.debug("Setting " + sdf.format(date) + " to beginning of day."); | |
| Calendar cal = Calendar.getInstance(Locale.US); | |
| cal.setTime(date); | |
| cal.set(Calendar.HOUR_OF_DAY, 0); | |
| cal.set(Calendar.MINUTE, 0); | |
| cal.set(Calendar.SECOND, 0); | |
| cal.set(Calendar.MILLISECOND, 0); | |
| log.debug("Time set for " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| /** | |
| * Sets the given date's time to 23:59:59:999 | |
| * | |
| * @param date | |
| * @return date modified to end of day | |
| */ | |
| public static Date setTimeToEndOfDay(Date date) { | |
| log.debug("Setting " + sdf.format(date) + " to end of day."); | |
| Calendar cal = Calendar.getInstance(Locale.US); | |
| cal.setTime(date); | |
| cal.set(Calendar.HOUR_OF_DAY, 23); | |
| cal.set(Calendar.MINUTE, 59); | |
| cal.set(Calendar.SECOND, 59); | |
| cal.set(Calendar.MILLISECOND, 999); | |
| log.debug("Time set for " + sdf.format(date)); | |
| return cal.getTime(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.recursivechaos.rcutils; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import org.apache.commons.logging.Log; | |
| import org.apache.commons.logging.LogFactory; | |
| /** | |
| * Utilities for creating and manipulating files. | |
| * | |
| * @author Andrew Bell | |
| * | |
| */ | |
| public class RcFileUtil { | |
| private static Log log = LogFactory.getLog(RcFileUtil.class); | |
| /** | |
| * Creates a new file, in the provided directory, with the provided label prepended to the | |
| * timestamp, and the provided extension. | |
| * | |
| * @param directory for the file to be created in | |
| * @param label text to be prepended to the timestamp | |
| * @param ext file extension, with the "." | |
| * @return a newly created file, as per provided information | |
| * @throws RcUtilException Invalid data passed for file creation | |
| */ | |
| public static File createNewTimestampedFile(String directory, String label, String ext) throws RcUtilException{ | |
| File newFile = null; | |
| // Create timestamp | |
| SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SS"); | |
| Date today = new Date(); | |
| String timestamp = fmt.format(today); | |
| // Verify input | |
| if(!verifyDirectory(directory)){ | |
| throw new RcUtilException(directory + " is not a valid directory."); | |
| } | |
| if(!verifyValidFileName(label)){ | |
| throw new RcUtilException(label + " cannot be used in a file name."); | |
| } | |
| if(!verifyValidExtension(ext)){ | |
| throw new RcUtilException(ext + " is not a valid extension."); | |
| } | |
| // Build path | |
| String path = directory + File.separator + label + "_" + timestamp + "." + ext; | |
| newFile = new File(path); | |
| log.debug("File created: " + newFile.getAbsolutePath()); | |
| return newFile; | |
| } | |
| /** | |
| * Verifies that the provided string is a valid extension, that is less than 5 chars and | |
| * starts with a "." | |
| * @param ext to be checked | |
| * @return true if a valid extension | |
| */ | |
| public static boolean verifyValidExtension(String ext) { | |
| Boolean valid = true; | |
| // Needs to start with a "." | |
| if(!ext.startsWith(".")){ | |
| valid = false; | |
| } | |
| // Length should be less that 5 | |
| if(ext.length()>5){ | |
| valid = false; | |
| } | |
| return valid; | |
| } | |
| /** | |
| * Verify if the given string can be used as a valid file name. | |
| * @param name filename to be checked | |
| * @return true if valid file name | |
| */ | |
| public static boolean verifyValidFileName(String name) { | |
| Boolean valid = true; | |
| if((name.contains("/")) || (name.contains("\\"))){ | |
| valid = false; | |
| } | |
| return valid; | |
| } | |
| /** | |
| * Checks to see if the provided string is a valid directory. | |
| * @param directory string of the directory location | |
| * @return true if a valid directory | |
| */ | |
| public static boolean verifyDirectory(String directory) { | |
| Boolean valid = true; | |
| File file = new File(directory); | |
| if (!file.isDirectory()) | |
| valid = false; | |
| if (!file.exists()){ | |
| valid = false; | |
| } | |
| return valid; | |
| } | |
| /** | |
| * Opens a file as a FileOutputStream | |
| * @param file to be opened | |
| * @return a FileOutputStream of the given file | |
| * @throws RcUtilException File not found | |
| */ | |
| public static FileOutputStream getOutputStream(File file) throws RcUtilException { | |
| FileOutputStream fileOut; | |
| try { | |
| fileOut = new FileOutputStream(file); | |
| } catch (FileNotFoundException e) { | |
| throw new RcUtilException("Unable to open/find file.", e); | |
| } | |
| return fileOut; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.recursivechaos.rcutils; | |
| /** | |
| * @author Andrew Bell | |
| * | |
| */ | |
| public class RcUtilException extends Exception { | |
| private static final long serialVersionUID = 1L; | |
| public RcUtilException() { super(); } | |
| public RcUtilException(String message) { super(message); } | |
| public RcUtilException(String message, Throwable cause) { super(message, cause); } | |
| public RcUtilException(Throwable cause) { super(cause); } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment