Sorting Month in Java with Set

Program:

package agn;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class Sorting {

	public static void main(String[] args) throws ParseException {
		Set<Date> myset = new TreeSet<Date>();
		DateFormat dateFormat = new SimpleDateFormat("MMM");
		myset.add(new java.sql.Date(dateFormat.parse("Apr").getTime()));
		myset.add(new java.sql.Date(dateFormat.parse("jan").getTime()));
		myset.add(new java.sql.Date(dateFormat.parse("dec").getTime()));
		myset.add(new java.sql.Date(dateFormat.parse("feb").getTime()));
		myset.add(new java.sql.Date(dateFormat.parse("jul").getTime()));
		myset.add(new java.sql.Date(dateFormat.parse("sep").getTime()));
		Iterator iter = myset.iterator();
		 DateFormat df = new SimpleDateFormat("MMM");
		while(iter.hasNext()){
			System.out.println(df.format(iter.next()));
		}

	}
}

 

Output:

Jan
Feb
Apr
Jul
Sep
Dec

Thanks for reading this post……!!!

642 total views, 1 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in