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

Bar Code Generation In Java Using iText API

You need to download iText.jar from http://www.lowagie.com/itext in order to be able to run this Barcode Example. This tutorial is about generating various BarCode types using Java and iText API. The generated bar codes will then be exported to a PDF file. Here are the kinds of bar codes we will be generating:

      BarCode
      BarCode 128
      Barcode 39
      BarCode EAN
      BarCode EANS upplemental
      BarCode Inter 25
      BarCode Postnet
      BarCode PostnetPlanet

Here is the code that generates these bar codes for our website “www.sanjaal.com”

/**
 * @ Author Kushal Paudyal
 * Original Code referenced from Lowagie iText tutorial website.
 */
package com.sanjaal.utilities;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.Barcode39;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodeEANSUPP;
import com.lowagie.text.pdf.BarcodeInter25;
import com.lowagie.text.pdf.BarcodePostnet;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class BarCodeGenerator {
PdfContentByte contentByte;

public void generateBarCode() {
	/** Step 1: Create a Document*/
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	try {

		/** Step 2: Create PDF Writer*/
		PdfWriter writer = PdfWriter.getInstance(document,
				new FileOutputStream("sanjaalDotCom_BarCode1.pdf"));

		/** Step 3: Open the document so that we can write over it.*/
		document.open();

		/** Step 4: We have to create a set of contents.*/
		contentByte = writer.getDirectContent();

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.getDefaultCell().setFixedHeight(70);

		String myText = "www.sanjaal.com"; // Text to encode

		table.addCell("CODE 39");
		table.addCell(new Phrase(new Chunk(createBarCode39(myText
				.toUpperCase()), 0, 0)));

		table.addCell("CODE 39 EXTENDED");
		table.addCell(new Phrase(new Chunk(createBarcode39Extended(myText),
				0, 0)));

		table.addCell("CODE 128");
		table.addCell(new Phrase(new Chunk(createBarcode128(myText), 0,
						0)));

		table.addCell("CODE INTERLEAVED");
		String myTextNum = "12345";
		table.addCell(new Phrase(new Chunk(createBarcodeInter25(myTextNum),
				0, 0)));

		table.addCell("CODE POSTNET");
		table.addCell(new Phrase(new Chunk(createBarcodePostnet(myTextNum),
				0, 0)));

		table.addCell("CODE PLANET");
		table.addCell(new Phrase(new Chunk(
				createBarcodePostnetPlanet(myTextNum), 0, 0)));

		String myTextEAN13 = "1234567890123";
		table.addCell("CODE EAN");
		table.addCell(new Phrase(new Chunk(createBarcodeEAN(myTextEAN13),
				0, 0)));

		table.addCell("CODE EANnWITHnSUPPLEMENTAL 5");
		table.addCell(new Phrase(new Chunk(createBARCodeEANSUPP(
				myTextEAN13, "12345"), 0, 0)));

		document.add(table);
	} catch (Exception de) {
		de.printStackTrace();
	}

	// step 5: we close the document
	document.close();
}

public static void main(String args[]) {
	new BarCodeGenerator().generateBarCode();
}

/**
 * Method to create barcode image of type Barcode39 for mytext
 */
public Image createBarCode39(String myText) {
	/**
	 * Code 39 character set consists of barcode symbols representing
	 * characters 0-9, A-Z, the space character and the following symbols:
	 * - . $ / + %
	 */

	Barcode39 myBarCode39 = new Barcode39();
	myBarCode39.setCode(myText);
	myBarCode39.setStartStopText(false);
	Image myBarCodeImage39 = myBarCode39.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImage39;
}

/**Creating a barcode image using Barcode39 extended type for myText*/
public Image createBarcode39Extended(String myText) {
	Barcode39 myBarCode39extended = new Barcode39();
	myBarCode39extended.setCode(myText);
	myBarCode39extended.setStartStopText(false);
	myBarCode39extended.setExtended(true);
	Image myBarCodeImage39Extended = myBarCode39extended
			.createImageWithBarcode(contentByte, null, null);
	return myBarCodeImage39Extended;
}

/** Creating a barcode image using Barcode 128 for myText*/
public Image createBarcode128(String myText) {
	Barcode128 code128 = new Barcode128();
	code128.setCode(myText);
	Image myBarCodeImage128 = code128.createImageWithBarcode(contentByte,
			null, null);
	return myBarCodeImage128;
}

/** Creating a barcode image using BarcodeEAN for myText*/
public Image createBarcodeEAN(String myText) {
	BarcodeEAN myBarcodeEAN = new BarcodeEAN();
	myBarcodeEAN.setCodeType(Barcode.EAN13); // 13 characters.
	myBarcodeEAN.setCode(myText);
	Image myBarCodeImageEAN = myBarcodeEAN.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImageEAN;
}

/** creating a barcode image using BarCodeInter25 for myText*/
public Image createBarcodeInter25(String myText) {
	BarcodeInter25 myBarcode25 = new BarcodeInter25();
	myBarcode25.setGenerateChecksum(true);
	myBarcode25.setCode(myText);
	Image myBarCodeImageInter25 = myBarcode25.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImageInter25;
}

/**creating a barcode image using BarcodePostnet for myText*/
public Image createBarcodePostnet(String myText) {
	BarcodePostnet myBarcodePostnet = new BarcodePostnet();
	myBarcodePostnet.setCode(myText);
	Image myBarcodeImagePostnet = myBarcodePostnet.createImageWithBarcode(
			contentByte, null, null);
	return myBarcodeImagePostnet;
}

/** creating a barcode image using BarCodeInter25 */
public Image createBarcodePostnetPlanet(String myText) {
	BarcodePostnet myBarCodePostnetPlanet = new BarcodePostnet();
	myBarCodePostnetPlanet.setCode(myText);
	myBarCodePostnetPlanet.setCodeType(Barcode.PLANET);
	Image myBarCodeImagePostntPlanet = myBarCodePostnetPlanet
			.createImageWithBarcode(contentByte, null, null);
	return myBarCodeImagePostntPlanet;
}

public Image createBARCodeEANSUPP(String myTextPrimary,
		String myTextSupplementary5) {
	PdfTemplate pdfTemplate = contentByte.createTemplate(0, 0);
	BarcodeEAN myBarcodeEAN = new BarcodeEAN();
	myBarcodeEAN.setCodeType(Barcode.EAN13);
	myBarcodeEAN.setCode(myTextPrimary);
	PdfTemplate ean = myBarcodeEAN.createTemplateWithBarcode(contentByte,
			null, Color.blue);
	BarcodeEAN codeSUPP = new BarcodeEAN();
	codeSUPP.setCodeType(Barcode.SUPP5);
	codeSUPP.setCode(myTextSupplementary5);
	codeSUPP.setBaseline(-2);
	BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(myBarcodeEAN, codeSUPP);
	Image imageEANSUPP = eanSupp.createImageWithBarcode(contentByte, null,
			Color.blue);
	return imageEANSUPP;
}
}

Simple Tag Generator In Java For Music Blogs

For those of you who run a music blog, you might have faced the tiresome job of writing bunch of tags, I have written this simple program in Java that generates the tags for your use. I personally developed this for my one of my Music Blogs, and I find this quite useful to generate a bunch of tags at once which I can copy paste to my WordPress based blog.

You can customize this program as required to suite your needs.

You can download the original file  SimpleTagGenerator.java for free and use it for any of your purposes.

package com.kushal.utils;
/**
 * @author KushalP
 * For those who maintain music blog, this simple program
 * might be useful for generating music tags.
 *
 * I developed this for my personal use for my following blog:
 * http://www.sanjaal.com/nepalisongs
 */

public class SimpleTagGenerator {

	public static void main(String args [])
	{
		/**
		 * Param 1 : Album Name
		 * Param 2 : Artist Name
		 * Param 3 : Song Name
		 */
		generateTag("Division Bell", "Pink Floyd", "Marooned");
	}

	public static void generateTag(String album, String artist, String song)
	{
		String artistTags="";
		String albumTags="";
		String songTags="";

		if(artist!=null && artist.length() >0 )
		{
			artistTags+=artist +" Guitar Tabs, "+
						artist+ " Latest Songs, "+
						artist+ " MP3 Downloads,"+
						artist+ " Latest Albums, "+
						artist+ " Songs Download, "+
						artist+ " Lyrics Download, "+
						artist+ " MP3 Songs";
		}

		if(album !=null && album.length() > 0)
		{
			albumTags+=artist +" "+ album+" "+ "Songs Download, "+
						artist +" " +album+" "+  "MP3 Download,"+
						artist +" "+ album+" "+ "Lyrics Download, "+
						artist + " "+album+" "+  "Guitar Tabs," +
						artist +" "+album+" "+ "Guitar Chords,";		

		}

		if(song!=null && song.length() > 0 )
		{
			songTags+=	artist +" "+song+" "+"Guitar Tabs, "+
						artist +" "+song+" "+ "MP3 Downloads,"+
						artist +" "+song+" "+ "Songs Download,"+
						artist +" "+song+" "+ "Guitar Chords,"+
						artist +" "+song+" "+ "MP3 Song,"+
						artist +" "+song+" "+ "Lyrics Download,"+
						artist +" "+song+" "+ "Music Video";		

		}

		System.out.println(artistTags+","+albumTags+","+songTags);
	}

}


Here are the sample tags generated for the the parameters that I passed in the application while calling generateTag method i.e. generateTag(“Division Bell”, “Pink Floyd”, “Marooned”);

Pink Floyd Guitar Tabs, Pink Floyd Latest Songs, Pink Floyd MP3 Downloads,Pink Floyd Latest Albums, Pink Floyd Songs Download, Pink Floyd Lyrics Download, Pink Floyd MP3 Songs,Pink Floyd Division Bell Songs Download, Pink Floyd Division Bell MP3 Download,Pink Floyd Division Bell Lyrics Download, Pink Floyd Division Bell Guitar Tabs,Pink Floyd Division Bell Guitar Chords,,Pink Floyd Marooned Guitar Tabs, Pink Floyd Marooned MP3 Downloads,Pink Floyd Marooned Songs Download,Pink Floyd Marooned Guitar Chords,Pink Floyd Marooned MP3 Song,Pink Floyd Marooned Lyrics Download,Pink Floyd Marooned Music Video