format Date by appending date parts together - Android java.util

Android examples for java.util:Date Format

Description

format Date by appending date parts together

Demo Code

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
  private static final TimeZone TIME_ZONE = TimeZone
      .getTimeZone("Asia/Tokyo");
  public static String formatDate(long date) {
    Calendar calendar = Calendar.getInstance(TIME_ZONE);
    calendar.setTimeInMillis(date);//from   w  ww. ja v a 2  s  .co m
    StringBuilder builder = new StringBuilder();
    String frmtDate = builder.append(calendar.get(Calendar.YEAR)).append("/").append(calendar.get(Calendar.MONTH) + 1)
        .append("/").append(calendar.get(Calendar.DATE)).append("\n").append(calendar.get(Calendar.HOUR_OF_DAY))
        .append(":").append(calendar.get(Calendar.DATE)).toString();
    return frmtDate;

  }

  public static String formatDate(Date date) {
    return formatDate(date.getTime());
  }

}

Related Tutorials