Mar 7, 2014

SimpleDateFormat




If you want show date,time in your program, can use this class.

'SimpleDateFormat' is useful class.

This class is java class.

But we use in android.

We can referenced Oracle document.

This figure is variable for SimpleDateFormat.


I introduce some examples.
If current date&time is 2014-03-07 12:00pm.

  1. Date date=new Date(System.currentTimeMillis());
  2. SimpleDateFormat CurTimeFormat=new SimpleDateFormat("hh:mm");
  3. String time=CurTimeFormat.format(date);
time==>12:00
If you want add 'am or pm'.

  1. Date date=new Date(System.currentTimeMillis());
  2. SimpleDateFormat CurtimeFormat=new SimpleDateFormat("a hh:mm");
  3. String time=CurtimeFormat.format(date);
time==> pm 12:00

If you want show current date
  1. Date date=new Date(System.currentTimeMillis());
  2. SimpleDateFormat CurDateFormat=new SimpleDateFormat("yyyy-MM-dd");
  3. String currentdate=CurDateFormat.format(date);
currentdate==> 2014-03-07

If you want add day in week, just add 'E'
  1. Date date=new Date(System.currentTimeMillis());
  2. SimpleDateFormat CurDateFormat=new SimpleDateFormat("YYYY-MM-dd E");
  3. String currentdate=CurDateFormat.format(date);
currentdate==> 2014-03-07 Fri

No comments:

Post a Comment