String source="01/03/2014"; SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); java.sql.Date estaDate= new java.sql.Date(format.parse(source).getTime());
Utilities
Check Validate User
class UserDAO { public static String[] getUsers() { String [] usernames = new String[] { "sridhar", "senthil", "siva" , "kamal" }; return usernames; } public static boolean isValidUser(String username) { String[] users = getUsers(); boolean isValid = false; for (String name : users) { if ( name.equals(username )) { isValid = true; break; } } return isValid; } } public class TestLogin { public static void main(String[] args) { //success case String username1 = "sridhar"; System.out.println( username1 + " is a Valid User ? " + UserDAO.isValidUser(username1)); //failure case String username2 = "abc"; System.out.println( username2 + " is a Valid User ? " + UserDAO.isValidUser(username2)); } }
Date Util
Obtaining Local Date and Time
import java.time.LocalDate; import java.time.LocalTime; public class LocalDateTime { static LocalDate localDate = LocalDate.now(); static LocalTime localTime = LocalTime.now(); public static void main(String[] args){ System.out.println("The local date is: " + localDate); System.out.println("The local time is:" + localTime); } }
DateTimeFormat
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class TestDateTimeApi{ public static void main (String[] args){ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate yearStart = LocalDate.parse("01/01/2014", formatter); System.out.println("Beginning of year: " + yearStart); } }
TimeIntervalExamples
import java.time.LocalDate; import java.time.Month; import java.time.Period; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; import java.util.concurrent.TimeUnit; /** * * Finding the Interval Between Dates and Times * */ public class TimeIntervalExamples { public static void intervals(){ LocalDate anniversary = LocalDate.of(2000, Month.NOVEMBER, 11); LocalDate today = LocalDate.now(); Period period = Period.between(anniversary, today); System.out.println("Number of Days Difference:" + period.getDays()); System.out.println("Number of Months Difference: " + period.getMonths()); System.out.println("Number of Years Difference: " + period.getYears()); } public static void totalsBetween(){ LocalDate anniversary = LocalDate.of(2000, Month.NOVEMBER, 11); LocalDate today = LocalDate.now(); long yearsBetween = ChronoUnit.YEARS.between(anniversary, today); System.out.println("Years between dates: " + yearsBetween); long daysBetween = ChronoUnit.DAYS.between(anniversary, today); System.out.println("Days between dates:" + daysBetween); } public static void compareDatesCalendar() { // Obtain two instances of the Calendar class Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); // Set the date to 01/01/2010:12:00 cal2.set(2010, 0, 1, 12, 0); Date date1 = cal2.getTime(); System.out.println(date1); long mill = Math.abs(cal1.getTimeInMillis() - date1.getTime()); // Convert to hours long hours = TimeUnit.MILLISECONDS.toHours(mill); // Convert to days Long days = TimeUnit.HOURS.toDays(hours); String diff = String.format("%d hour(s) %d min(s)", hours, TimeUnit.MILLISECONDS.toMinutes(mill) - TimeUnit.HOURS.toMinutes(hours)); System.out.println(diff); diff = String.format("%d days", days); System.out.println(diff); // Divide the number of days by seven for the weeks int weeks = days.intValue() / 7; diff = String.format("%d weeks", weeks); System.out.println(diff); } public static void main(String[] args){ intervals(); totalsBetween(); compareDatesCalendar(); }
Date Comparison
import java.time.LocalDate; import java.time.Month; /** * * Compare Two Dates * */ public class DateComparisons { public static void compareDates(LocalDate ldt1, LocalDate ldt2) { int comparison = ldt1.compareTo(ldt2); if (comparison > 0) { System.out.println(ldt1 + " is larger than " + ldt2); } else if (comparison < 0) { System.out.println(ldt1 + " is smaller than " + ldt2); } else { System.out.println(ldt1 + " is equal to " + ldt2); } } public static void main(String[] args) { LocalDate anniversary = LocalDate.of(2000, Month.NOVEMBER, 11); LocalDate today = LocalDate.now(); compareDates(anniversary, today); } }
DB Connection Util
public class ConnectionUtil { public static Connection getConnection() throws Exception { Connection con = null; String url = "jdbc:mysql://localhost/SampleDB"; String userName = "root"; String passWord = ""; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, userName, passWord); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } return con; } public static void close(Connection conn , Statement stmt, ResultSet rs){ try { if ( rs != null ){ rs.close(); } if ( stmt != null ) { stmt.close(); } if ( conn != null ){ conn.close(); } } catch(SQLException e){ } } }