Formatting month using
Simple Date Format
import
java.text.SimpleDateFormat;
import java.util.Date;
public class
FormattingMonth
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat =
"M";
SimpleDateFormat sdf =
new SimpleDateFormat(strDateFormat);
System.out.println("Current
Month in M format : " + sdf.format(date));
strDateFormat =
"MM";
sdf = new
SimpleDateFormat(strDateFormat);
System.out.println("Current
Month in MM format : " + sdf.format(date));
strDateFormat =
"MMM";
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current
Month in MMM format : " + sdf.format(date));
strDateFormat =
"MMMM";
sdf = new
SimpleDateFormat(strDateFormat);
System.out.println("Current
Month in MMMM format : " + sdf.format(date));
}
}
Formatting seconds using
Simple Date Format
import
java.text.SimpleDateFormat;
import java.util.Date;
public class
FormattingSeconds
{
public static void main(String[] args)
{
Date date = new Date();
String strDateFormat =
"s";
SimpleDateFormat sdf =
new SimpleDateFormat(strDateFormat);
System.out.println("seconds
in s format : " + sdf.format(date));
strDateFormat =
"ss";
sdf = new
SimpleDateFormat(strDateFormat);
System.out.println("seconds
in ss format : " + sdf.format(date));
}
}
Formatting year using
Simple Date Format
import
java.text.SimpleDateFormat;
import java.util.Date;
public class
FormattingYear
{
public static void
main(String[] args)
{
Date date = new Date();
String strDateFormat =
"yy";
SimpleDateFormat sdf =
new SimpleDateFormat(strDateFormat);
System.out.println("Current
year in yy format : " + sdf.format(date));
strDateFormat =
"yyyy";
sdf = new
SimpleDateFormat(strDateFormat);
System.out.println("Current
year in yyyy format : " + sdf.format(date));
}
}
Java Simple Date Format
Class
import
java.text.SimpleDateFormat;
import java.util.Date;
public class JavaSimpleDateFormatExample
{
public static void
main(String args[])
{
Date date = new Date();
String Date_Format =
"MM/dd/yyyy";
SimpleDateFormat sdf =
new SimpleDateFormat(Date_Format);
System.out.println("Today
is " + sdf.format(date) );
}
}
Parsing custom formatted
date string into Date object using Simple Date Format
import java.util.Date;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
public class
ParsingCustomFormatDate
{
public static void
main(String[] args)
{
SimpleDateFormat sdf = new
SimpleDateFormat("dd/MM/yy");
try
{
Date date =
sdf.parse("31/12/06");
System.out.println(date);
}
catch(ParseException pe)
{
System.out.println("Parse
Exception : " + pe);
}
}
}
No comments:
Post a Comment