Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Formating date in Current / any Locale in Java with Style Example code

DateFormat helps to format and parse dates for any locale . DateFormat has many methods for obtaining default date / time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include SHORT (23/07/11 ) , MEDIUM ( Jul 23, 2011 ), LONG (July 23, 2011 ) , FULL (Saturday, 23 July 2011 ). DateFormat has a method getDateInstance() which is used to get the normal date format for that country

To format a date in the current Locale,

dateFormatStr = DateFormat.getDateInstance().format(myDate);

To format a date for a different Locale, specify it in the call to getDateInstance().

DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);

The following code formats the current date into various different locales with different styles (SHORT, MEDIUM, LONG, FULL)

  

import java.util.*;
import java.text.*;

public class DateFormatLocale

{
public static void main(String args[])
{
Date today = new Date();

Locale[] locales ={Locale.CHINA, Locale.UK, Locale.GERMANY, Locale.FRANCE, Locale.US };
int[] fmt_styles={ DateFormat.SHORT,DateFormat.MEDIUM,DateFormat.LONG, DateFormat.FULL};
DateFormat format;
String[] fmt_style_str={ "SHORT", "MEDIUM", "LONG", "FULL"};
for (int i=0; i<locales.length;i++)
{
System.out.println("\n The date format for the country " + locales[i].getDisplayCountry() );
for (int j=0; j<fmt_styles.length;j++)
{
format = DateFormat.getDateInstance(fmt_styles[j], locales[i]);
System.out.println("\n " + fmt_style_str[j] + " is " + format.format(today));

}
}

String dateCurrentLocale = DateFormat.getDateInstance().format(today);

System.out.println("Date format for the current locale " + dateCurrentLocale);

}
}

Output of the Program:

java DateFormatLocale

The date format for the country China

SHORT is 7/23/11

MEDIUM is Jul 23, 2011

LONG is July 23, 2011

FULL is Saturday, July 23, 2011

The date format for the country United Kingd

SHORT is 23/07/11

MEDIUM is 23-Jul-2011

LONG is 23 July 2011

FULL is Saturday, 23 July 2011

The date format for the country Germany

SHORT is 23.07.11

MEDIUM is 23.07.2011

LONG is 23. Juli 2011

FULL is Samstag, 23. Juli 2011
....
......


Read more...

Java String to Date , Date to String Example code using SimpleDateFormat , parse , format

SimpleDateFormat Class is used for Parsing ( String to Date ) and Formatting (Date to String ) . It has many methods . For converting String to Date and vice-versa , the following methods are used .

1) parse(String text, ParsePosition pos) - Parses text from a string to convert to a Date
2) format(Date date, StringBuffer toAppendTo, FieldPosition pos) - Formats the given Date into a date/time string(which may be dd/MM/yyyy,dd/MMM/yyyy, yyyyy.MMMMM.dd GGG hh:mm aaa etc ) and appends the result to the given StringBuffer.

The following code converts spring to date and date to string ......
    
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatParse {

public static void main(String[] args) {

SimpleDateFormat out = new SimpleDateFormat("dd/MM/yyyy");

SimpleDateFormat out1 = new SimpleDateFormat("dd-MMM-yyyy");

System.out.println("String to Date : " + StringToDate(15, 06, 2011) );// which returns a date

System.out.println("String to Date : " + StringToDate("15/06/2011") );// which returns a date

// Now Date is converted into String for printing in a format (dd/mm/yyy or dd/MMM/yyyy)

System.out.println("Date to String : " + out.format (StringToDate(15, 06, 2011) ));// which returns a string

System.out.println("Date to String : " + out1.format (StringToDate("15/06/2011") )); // which returns a string

}



// The following two functions are used to convert string to date (One accepts (dd/mm/yyyy) , another method accepts day, month & year)




public static Date StringToDate(String dateString) // Accepts a Date String and Converts to Date
{
SimpleDateFormat in= new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
try {

date = in.parse(dateString);

} catch (ParseException e)
{
e.printStackTrace();
}
return date;
}


public static Date StringToDate(int day, int month, int year) // Accepts a Day , Month and Year and Converts to Date
{
SimpleDateFormat in = new SimpleDateFormat("dd/MM/yyyy");
String dateString = day + "/" + month + "/" + year;
Date date=new Date();
try {

date = in.parse(dateString);

} catch (ParseException e)
{
e.printStackTrace();
}
return date;
}
}


Output of the above program :

String to Date : Wed Jun 15 00:00:00 IST 2011
String to Date : Wed Jun 15 00:00:00 IST 2011
Date to String : 15/06/2011
Date to String : 15-Jun-2011


Read more...

 
Copyright 2011 Java Online