Monday, November 3, 2014

Java using date format



Formatting Minutes using Simple Date Format

import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattingMinute
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat = "m";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("minutes in m format : " + sdf.format(date));
strDateFormat = "mm";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("minutes in mm format : " + sdf.format(date));
    }
}
Formatting Time Zone using Simple Date Format
import java.text.SimpleDateFormat;
import java.util.Date; 
public class FormattingTimeZone
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat = "zzz";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("TimeZone in z format : " + sdf.format(date));
strDateFormat = "zzzz";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("TimeZone in zzzz format : " + sdf.format(date));
strDateFormat = "Z";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("TimeZone in Z format : " + sdf.format(date));
   }
}

Formatting date in default formats using Date Format

import java.text.DateFormat;
import java.util.Date;
public class FormattingDateInDefaultFormats
{
 public static void main(String[] args)
{
 Date date = new Date();
String strDate = DateFormat.getDateInstance(DateFormat.Short).format(date);
System.out.println(strDate);
strDate = DateFormat.getDateInstance(DateFormat.Medium).format(date);   System.out.println(strDate);    
strDate = DateFormat.getDateInstance(DateFormat.Long).format(date);
System.out.println(strDate);
strDate = DateFormat.getDateInstance(DateFormat.Full).format(date);
System.out.println(strDate);
    }
}

Formatting day of week using Simple Date Format

import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattingDayOfWeek
          {
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat = "E";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day of week in E format : " + sdf.format(date));
strDateFormat = "Eeee";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day of week in Eeee format : " + sdf.format(date));
    }
}

Formatting day using Simple Date Format

import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattingDay
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat = "d";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day in d format : " + sdf.format(date));
strDateFormat = "dd";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day in dd format : " + sdf.format(date));
    }
}

Formatting hour using Simple Date Format

import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattingHour
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat = "h";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("hour in h format : " + sdf.format(date));
     strDateFormat = "hh";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in hh format : " + sdf.format(date));
     strDateFormat = "H";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in H format : " + sdf.format(date));
strDateFormat = "HH";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in HH format : " + sdf.format(date));
strDateFormat = "k";
sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("hour in k format : " + sdf.format(date));
     strDateFormat = "kk";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in kk format : " + sdf.format(date));
strDateFormat = "K";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in K format : " + sdf.format(date));
strDateFormat = "KK";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in KK format : " + sdf.format(date));
   }
}

No comments:

Post a Comment