Util.java Source code

Java tutorial

Introduction

Here is the source code for Util.java

Source

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

public class Util {
    public static String formatAsMySQLDatetime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);

        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);

        return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    }

}