Last active
August 29, 2015 13:57
-
-
Save Brobin/9540663 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* A quick little group of methods that I mad eup while working ona java database project | |
* Dates in java are really annoying. These methods provide a quick and easy way to convert | |
* between different types and get the start and end dates for a given month. | |
* | |
* java.util.Date is the java class library for dates that is technically measured in seconds | |
* and has methods to assign integer values for day, month, and year | |
* | |
* java.sql.Date is the date library for dates entered into an SQL database using JDBC. It | |
* extends java.util.Date, so most of the methods can be reused, but conversions need to be made. | |
* | |
* @author Tobin Brown | |
* @version 1.0 | |
**/ | |
/** | |
* Converts a java.util.Date to a java.sql.Date | |
* @param java.util.Date date | |
* @return java.sql.Date | |
**/ | |
private java.sql.Date sqlDate(java.util.Date date) { | |
return new java.sql.Date(date.getTime()); | |
} | |
/** | |
* Converts a java.sql.Date to a java.util.Date | |
* @param java.sql.Date date | |
* @return java.util.Date | |
**/ | |
private java.util.Date utilDate(java.sql.Date date) { | |
return new java.util.Date(date.getTime()); | |
} | |
/** | |
* Gets the sql date object for the first day of a given month | |
* @param int month | |
* @param int year | |
* @return java.sql.Date startDate | |
**/ | |
private java.sql.Date getStartDate(int month, int year) throws ParseException { | |
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); | |
java.util.Date date = sdf.parse(month + "-01-" + year); | |
return sqlDate(date); | |
} | |
/** | |
* Gets the sql date object for the last day of a given month | |
* @param int month | |
* @param int year | |
* @return java.sql.Date endDate | |
**/ | |
private java.sql.Date getEndDate(int m, int year) throws ParseException { | |
int day; | |
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { | |
day = 31; | |
} else if (m == 2) { | |
day = 28; | |
} else { | |
day = 30; | |
} | |
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); | |
java.util.Date date = sdf.parse(m + "-" + day + "-" + year); | |
return sqlDate(date.getTime()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment