I posted before about different date classes in Java and conversions between them.
Sometimes I want to display date type in different formats in Java like DD-MM-RRRR , DD-MON-YYYY , MON-YY .... etc
I will post about about different Java Classes that can be used for this purpose.
1- java.text.SimpleDateFormat
2- java.text.DateFormat
3- org.apache.commons.lang.time.DateFormatUtils
1- SimpleDateFormat Class
I will use java.text.SimpleDateFormat class for formatting Date and time also
You can use anyone from below patterns
Example
2- DateFormat ClassI will use java.text.DateFormat class for formatting too
Example
Example
Thanks
Sometimes I want to display date type in different formats in Java like DD-MM-RRRR , DD-MON-YYYY , MON-YY .... etc
I will post about about different Java Classes that can be used for this purpose.
1- java.text.SimpleDateFormat
2- java.text.DateFormat
3- org.apache.commons.lang.time.DateFormatUtils
1- SimpleDateFormat Class
I will use java.text.SimpleDateFormat class for formatting Date and time also
You can use anyone from below patterns
Example
public static void main(String[] args) {
String pattern = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateAfterPattern = simpleDateFormat.format(new java.util.Date());
System.out.println("dateAfterPattern=" + dateAfterPattern);
}
2- DateFormat ClassI will use java.text.DateFormat class for formatting too
Example
String dateAfterFormat = DateFormat.getDateInstance(DateFormat.MEDIUM).format((new java.util.Date()));
System.out.println("dateAfterPattern=" + dateAfterFormat);
3- DateFormatUtils Class
I will use org.apache.commons.lang.time.DateFormatUtils helper class from formatting date and time.Example
public static void main(String[] args) {
String pattern = "dd-MM-yyyy";
String dateAfterPattern = DateFormatUtils.format((new java.util.Date()), "MM/dd");
System.out.println("dateAfterPattern=" + dateAfterPattern);
}
Thanks
Another option is to use Joda Time library (http://joda-time.sourceforge.net/userguide.html#Formatters).
ReplyDeleteYou can also use String.format() (see http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#dt)
ReplyDeleteOr you can simply use PrettyTime
ReplyDeletehttp://ocpsoft.org/prettytime/
in 3rd example String pattern = "dd-MM-yyyy";
ReplyDeleteis unused variable
please write somewhere that SimpleDateFormat is NOT THREADSAFE; I wouldn't want someone who learns it from this post to use it a public static final...
ReplyDeleteAlso Apache's commons Lang FastDateFormat is worth mentioning since it is almost a drop-in replacement for SimpleDateFormat and IS threadsafe.
Welcome to our website
ReplyDeletejava training that is the largest professional java training site where java,
java training, online java course, java training in Virginia,
java programming are available
more detail.......
online java course
java programming
java training in Virginia
Exactly, you're very kind of us about comment!.
ReplyDelete代写cs 北美
Enroll for Core Java Training and employ your talent on different IDEs. Earn about the very best 5 IDEs to apply Java. Online Advanced JAVA Training
ReplyDeleteThank you for shharing this
ReplyDelete