Creating java.util.date and java.util.sql objects from Calendar object

Date, time and calendar manipulation always seem to trouble average developers. Well it does for me too, sometimes. In this particular very simple tutorial, I have shown how we can create java.sql.date object and java.util.date object from Calendar object.

package com.kushal.utils.date;
import java.util.Calendar;

public class DateUsingCalendar {
	/**
	 * @param year - an integer representing the year
	 * @param month - an integer from 0-11 (Jan - Dec)
	 * @param dayOfMonth - an integer from 1-31
	 */
	public static void createDateFromCalendar(int year, int month,
			int dayOfMonth) {

		/**
		 * Creates a current calendar using
		 * the default time zone and locale.
		 */
		Calendar myCalendar = Calendar.getInstance();

		/**
		 * Resets all the attributes of calendar
		 */
		myCalendar.clear();

		/**
		 * Let's try setting our values
		 */
		myCalendar.set(Calendar.YEAR, year);
		myCalendar.set(Calendar.MONTH, month);
		myCalendar.set(Calendar.DATE, dayOfMonth);

		/**
		 * We will try creating a new sqlDate using this
		 * Calendar Object
		 */
		java.sql.Date sqlDateObj = new java.sql.Date(
				myCalendar.getTimeInMillis());

		/**
		 * We will try creating a new java util Date object
		 * using this Calendar Object
		 */
		java.util.Date utilDateObj = myCalendar.getTime();

		/**
		 * Let's try printing these dates to the console
		 */

		System.out.println("Date in sql date format: "+sqlDateObj);
		System.out.println("Date in util date format: "+utilDateObj);

	}

	public static void main(String[] args) {
		int year=2009;
		int month=5; //June (January being 0)
		int dayOfMonth=11;
		createDateFromCalendar(year, month, dayOfMonth);
	}
}


_______________________
Here is the output:
Date in sql date format: 2009-06-11
Date in util date format: Thu Jun 11 00:00:00 CDT 2009

  1. How to extract plain Text from HTML Website easily in Java
  2. Removing Time Portion From A Date In Java And Setting Custom Time To A Date
  3. How To Find What Java Version You Are Using?
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Creating java.util.date and java.util.sql objects from Calendar object
  6. Bar Code Generation In Java Using iText API
  7. Simple Tag Generator In Java For Music Blogs
  8. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  9. Calling URL Browser From Java Application
  10. Java Reflection Tutorial – List Methods Of A Class
  11. Cleaning Punctuation And White Spaces From A String
  12. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  13. Prime Number Finder In Java
  14. Utility Class For Getting SQL Connection and Closing It
  15. How to calculate age in Java from birthdate?
  16. Java Design Pattern – Singleton Pattern
  17. Reading / Writing File in Java and String Manipulation
  18. How to capture a screenshot using Java and save it to a file?
  19. Calculating The Difference Between Two Dates In Terms of Months
  20. How To Get All Supported Timzone IDs, Display Name and GMT Offset
  21. Getting The Name Of Currently Logged In User In The System
  22. How To Set And Get System Clipboard Contents In Java
  23. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  24. Generating Nepali Unicode Sequence For Characters
  25. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  26. Replacing non-ASCII characters using Java Regular Expressions
  27. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: Sanjaal.com » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    {
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    total+=pathname.length();
    } else {
    pathname.listFiles(this);
    }
    return false;
    }
    public long getTotal()
    {
    return total;
    }
    }

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    file.listFiles(counter);
    return counter.getTotal();
    }

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply

Your email address will not be published. Required fields are marked *