Java Code Examples for java.sql.Date

Following code examples demonstrate how to use java.sql.Datefrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.sql.Dateand various code implementation of this class.

	public static void main(String[] args) {
		java.sql.Date fromDate=new Date(2018,10,10);
		java.sql.Date toDate=new java.sql.Date(2030,10,20);
		
		Map<String,java.sql.Date> dateMap=new HashMap<String, java.sql.Date>();
		dateMap.put("frmDate", fromDate);
		dateMap.put("toDate", toDate);
		
		if(dateMap.containsKey("frmDate")) {
			java.sql.Date dbfromdate=dateMap.get("frmDate");
			System.out.println("SqlDateMapExample.main() dbfrmdta="+dbfromdate);
		}
		
		if(dateMap.containsKey("toDate")) {
			java.sql.Date toDbDate=dateMap.get("toDate");
			System.out.println("SqlDateMapExample.main() dbtodate="+toDbDate);
		}
		
		Map<String,Object> objectDateMap=new HashMap<String, Object>();
		objectDateMap.put("objFromDate", fromDate);
		if(objectDateMap.containsKey("objFromDate")) {
			java.sql.Date objFromDate=(Date) objectDateMap.get("objFromDate");
			System.out.println("SqlDateMapExample.main() objfroDate="+objFromDate);
		}
	} 


	public void test_start_after_before() {
		Date startDate = java.sql.Date.valueOf("2016-04-02");
		Date endDate = java.sql.Date.valueOf("2015-04-01");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertEquals(0.00, resultValue, 0);
	} 

	public void test_getNumberOfMonth_1_month() {
		Date startDate = java.sql.Date.valueOf("1990-12-01");
		Date endDate = java.sql.Date.valueOf("1990-12-31");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertEquals(1.00, resultValue, 0);
	} 

	public void test_get12MonthPeriod_notFirstOfMonth() {
		Date startDate = java.sql.Date.valueOf("2015-04-02");
		Date endDate = java.sql.Date.valueOf("2016-04-01");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertEquals(12.00, resultValue, 0);
	} 

	public void test_getNumberOfMonth() {
		Date startDate = java.sql.Date.valueOf("1990-11-30");
		Date endDate = java.sql.Date.valueOf("1990-12-30");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertEquals(1.03, resultValue, 0);
	} 

	public void test_getNumberOfMonthInDecimal() {
		Date startDate = java.sql.Date.valueOf("2015-01-01");
		Date endDate = java.sql.Date.valueOf("2016-03-31");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertEquals(15.0, resultValue, 0);
	} 

	public void test_getNumberOfMonthsInvalid() {
		Date startDate = java.sql.Date.valueOf("2015-01-20");
		Date endDate = java.sql.Date.valueOf("2016-01-13");
		double resultValue = proposalBudgetNumberOfMonthsService
				.getNumberOfMonth(startDate, endDate);
		assertNotEquals(12.13, resultValue, 0);
		assertEquals(11.81, resultValue, 0);
	} 

    public static boolean equals(java.sql.Date sqlDate, java.sql.Date... anotherSQLDates) {
        for(Date anotherSQLDate : anotherSQLDates)
            if(notEquals(sqlDate, anotherSQLDate))
                return false;
        return true;
    } 

    public static boolean after(java.sql.Date sqlDate, java.sql.Date... anotherSQLDates) {
        if(equals(sqlDate, anotherSQLDates))
            return false;
        for(Date anotherSQLDate : anotherSQLDates)
            if(before(sqlDate, anotherSQLDate))
                return false;
        return true;
    } 

    public static boolean notEquals(java.sql.Date sqlDate, java.sql.Date... anotherSQLDates) {
        for(Date anotherSQLDate : anotherSQLDates)
            if(equals(sqlDate, anotherSQLDate))
                return false;
        return true;
    } 

    public static boolean before(java.sql.Date sqlDate, java.sql.Date... anotherSQLDates) {
        if(equals(sqlDate, anotherSQLDates))
            return false;
        for(Date anotherSQLDate : anotherSQLDates)
            if(after(sqlDate, anotherSQLDate))
                return false;
        return true;
    } 

	private void createDatePicker() {
		datePicker = new DatePicker();
		datePicker.setConverter(converter);
		if (this.getItem()!=null){
			
			datePicker.setValue(((java.sql.Date) this.getItem()).toLocalDate());
		} else {
			datePicker.setValue(LocalDate.now());
		}
		datePicker.setPrefWidth(this.getWidth() - this.getGraphicTextGap() * 2);
		datePicker.setEditable(this.datePickerEditable);
		datePicker.valueProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue prop, Object oldValue, Object newValue) {
				if (DatePickerTableCell.this.isEditing()) {
					DatePickerTableCell.this.commitEdit((java.sql.Date) Date.valueOf(newValue.toString()));
				}
			}
		});
	} 

			public void changed(ObservableValue prop, Object oldValue, Object newValue) {
				if (DatePickerTableCell.this.isEditing()) {
					DatePickerTableCell.this.commitEdit((java.sql.Date) Date.valueOf(newValue.toString()));
				}
			} 

	public static String conversionPourSQL(java.sql.Date date){

		String stringDate = "";

		GregorianCalendar calEmprunt =  new GregorianCalendar();
		calEmprunt.setTime(date);
		
		Integer intNumeroJour = calEmprunt.get(Calendar.DAY_OF_MONTH);
		String numeroJour = "";
		if(intNumeroJour.toString().length() == 1){
			numeroJour = "0" + calEmprunt.get(Calendar.DAY_OF_MONTH);
		}
		else{
			numeroJour = "" + calEmprunt.get(Calendar.DAY_OF_MONTH);
		}
		
		Integer intNumeroMois = calEmprunt.get(Calendar.MONTH) + 1;
		String numeroMois = "";
		if(intNumeroMois.toString().length() == 1){
			numeroMois = "0" + intNumeroMois;
		}
		else{
			numeroMois = "" + intNumeroMois;
		}
		
		String numeroAnnee = ""+calEmprunt.get(Calendar.YEAR);
		
		
		Integer intNumeroHeure = calEmprunt.get(Calendar.HOUR_OF_DAY);
		String numeroHeure = "";
		if(intNumeroHeure.toString().length() == 1){
			numeroHeure = "0" + calEmprunt.get(Calendar.HOUR_OF_DAY);
		}
		else{
			numeroHeure = "" + calEmprunt.get(Calendar.HOUR_OF_DAY );
		}
		
		Integer intNumeroMinute = calEmprunt.get(Calendar.MINUTE);
		String numeroMinute = "";
		if(intNumeroMinute.toString().length() == 1){
			numeroMinute = "0" + calEmprunt.get(Calendar.MINUTE);
		}
		else{
			numeroMinute = "" + calEmprunt.get(Calendar.MINUTE);
		}
		
		stringDate = numeroJour+"-"+numeroMois+"-"+numeroAnnee+" "+numeroHeure+":"+numeroMinute;
		return stringDate;
		
	} 

	public static String Presenter(Date date) {
		
		GregorianCalendar gCal = new GregorianCalendar();
		gCal.setTime(date);
		
		String dateAPresenter =gCal.get(Calendar.DATE)+" "+gCal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.FRENCH)+" "+gCal.get(Calendar.YEAR);
		return dateAPresenter;
	} 

    public static int daysBetween(Calendar startDate, Calendar endDate) {
        Calendar date = (Calendar) startDate.clone();
        int daysBetween = 0;
        while (date.before(endDate)) {
            date.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return daysBetween;
    }     

  public static void main(String args[]) throws ParseException {

    String strDate = "2011-12-31 00:00:00";

    

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    java.util.Date date = sdf.parse(strDate);

    java.sql.Date sqlDate = new Date(date.getTime());

    System.out.println("String converted to java.sql.Date :" + sqlDate);
  } 

	public static int getMonthDifference(String startDate, String endDate) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date fromDate = format.parse(startDate);
		java.util.Date toDate = format.parse(endDate);

		Calendar beginningDate = Calendar.getInstance();
		beginningDate.setTime(fromDate);

		Calendar endingDate = Calendar.getInstance();
		endingDate.setTime(toDate);

		int m1 = beginningDate.get(Calendar.YEAR) * 12 + beginningDate.get(Calendar.MONTH);
		int m2 = endingDate.get(Calendar.YEAR) * 12 + endingDate.get(Calendar.MONTH);
		int noOfmonths = m2 - m1 + 1;
		logger.debug("Months diffrence is " + noOfmonths);
		return noOfmonths;
	} 

	public static java.sql.Date getReducedSqlDatewithDays(String inputDate, int noOfDays) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date inputDateParsed = format.parse(inputDate);

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDateParsed);
		logger.debug("Current Date is" + c1.getTime());
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("Sql Date with reduced days is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

	public static java.sql.Date getSqlDateFromStringDate(String inputDate) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date inputDateParsed = format.parse(inputDate);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDateParsed);
		logger.debug("Current Date is" + c1.getTime());
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("New sql date is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

	public static int getDaysDifference(String startDate, String endDate) throws ParseException {
		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date fromDate = format.parse(startDate);
		java.util.Date toDate = format.parse(endDate);

		int noOfdays = (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
		noOfdays = noOfdays + 1;

		return noOfdays;
	} 

	public static java.sql.Date getExtendedSqlDatewithMonths(String inputDate, int noOfmonths) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date inputDateParsed = format.parse(inputDate);

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDateParsed);
		logger.debug("Current Date is" + c1.getTime());
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("Sql  with reduced months is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

	public static java.sql.Date getExtendedSqlDatewithDays(Date inputDate, int noOfDays) throws ParseException {

		logger.debug("Entered into DateUtility......");

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDate);
		logger.debug("Current Date is" + c1.getTime());
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("Sql Date with added days is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

	public static java.sql.Date getExtendedSqlDatewithDays(String inputDate, int noOfDays) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date inputDateParsed = format.parse(inputDate);

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDateParsed);
		logger.debug("Current Date is" + c1.getTime());
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("Sql Date with added days is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

	public static java.sql.Date getReducedSqlDatewithMonths(String inputDate, int noOfmonths) throws ParseException {

		logger.debug("Entered into DateUtility......");
		java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date inputDateParsed = format.parse(inputDate);

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inputDateParsed);
		logger.debug("Current Date is" + c1.getTime());
		c1.set(Calendar.DAY_OF_MONTH, 1);
		java.sql.Date reducedSqlDate = new java.sql.Date(c1.getTimeInMillis());
		logger.debug("Sql  with reduced months is" + reducedSqlDate.toString());
		return reducedSqlDate;

	} 

Advertisement
Javadoc
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A

milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

Read More
Advertisement