Java - Write code to Convert Date To String

Requirements

Write code to Convert Date To String

Return a string

Hint

Use StringBuilder and via Calendar

Demo

//package com.book2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] argv) {
        Date date = new Date();
        System.out.println(ConvertToString(date));
    }/*  www .  jav  a2 s  .  c o  m*/

    public static String ConvertToString(Date date) {

        StringBuilder sb = new StringBuilder();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        sb.append(calendar.get(Calendar.YEAR) + "/");

        int month = calendar.get(Calendar.MONTH) + 1;

        sb.append(month + "/" + calendar.get(Calendar.DAY_OF_MONTH));
        sb.append(calendar.get(Calendar.HOUR_OF_DAY) + "/"+ calendar.get(Calendar.MINUTE));

        return sb.toString();

    }
}

Related Exercise