Showing posts with label Java Utilities. Show all posts
Showing posts with label Java Utilities. Show all posts

Thursday, 14 November 2013

Get Season By Month and Date in Java

The following illustrates getting season name by month and date in Java.

import java.util.*;
import java.text.*;
class CheckSeason
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
       
        System.out.println("Enter the month and day");
        int m=s.nextInt();
        int d=s.nextInt();
       
       
        SimpleDateFormat f=new SimpleDateFormat("MM-dd");
        f.setLenient(false);
       
        try
        {
            Date date=f.parse(m+"-"+d);
           
                if(date.after(f.parse("04-21")) && date.before(f.parse("06-21"))){
                    System.out.println("Spring");
                }
                else if(date.after(f.parse("06-20")) && (date.before(f.parse("09-23"))))
                {
                    System.out.println("Summer");
                }
                else if(date.after(f.parse("09-22")) && date.before(f.parse("12-22")))
                {
                    System.out.println("Fall");
                }
                else System.out.println("Winter");
        }catch(Exception e){
            System.out.println("Invalid month/date "+e);
        }
       
    }
}
new SimpleDateFormat("MM-dd"): Create an object for the SimpleDateFormat class of the format month-date.
setLenient(false): Allow strict checking of the date, so that month cannot be greater than 12 and February contains no more than < 30 days always etc.
f.parse(m+"-"+d): This creates a java.util.Date object from given month and date in the specified format.
date.after(): This method checks if the parameter date is after the calling date, similiarily date.before() checks if the parameter date is before the calling date with the help of which we determine the season.

Wednesday, 13 November 2013

Get Currency Symbol by Country in Java

The following example illustrates getting currency symbol or code by country code in Java. This tiny program, which is not more than 2 lines, does the thing. In this example, you'll learn:
  1. How to create a Locale object by language and country name?
  2. How to create a Currency object from Locale object?
  3. How to get symbol of a currency?

import java.util.*;
class GetCurrencySymbol
{
    public static void main(String args[])
    {
    Currency c=Currency.getInstance(new Locale("en",args[0]));
    System.out.println("Symbol is "+c.getSymbol());
    }
}

new Locale("en",args[0]); This constructor takes language and country code as parameter and creates Locale object.
Currency.getInstance(): This method takes java.util.Locale object and returns a Currency object. This is a factory method.
getSymbol(): This method gets the symbol of the currency associated with the object.

Tuesday, 12 November 2013

How to shuffle elements in ArrayList?

Here is a single line code that lets you shuffle elements in an ArrayList. The method shuffle() in the Collections class lets you do this. Note that the line you write is no more than single line, there are a lot of lines written in the shuffle() method ;)


import java.util.*;
class ShuffleElements
{
    public static void main(String args[])
    {
        // Do nothing, if no arguments < 2
        if(args.length<2) return;
      
        // Create an array list containing the array
        // Arrays.asList() adds the given array to a List
        // and returns that object
        // The second constructor of ArrayList
        ArrayList<String> a=new ArrayList<>(Arrays.asList(args));
      
        // Shuffle the elements
        Collections.shuffle(a);
          
            // Print all of them
            for(int i=0;i<args.length;i++)
            {
                System.out.println(a.get(i));
            }
    }
}

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

“I must complain the cards are ill shuffled till I have a good hand” - Jonathan Swift