Format Date to String in yyyy-MM-dd HH-mm-ss format - Java java.util

Java examples for java.util:Date Format

Description

Format Date to String in yyyy-MM-dd HH-mm-ss format

Demo Code

//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] argv) {
        Date date = new Date();
        System.out.println(dateTimetoString(date));
    }//from  w w  w.j ava2  s.c  om

    public static String dateTimetoString(Date date) {
        if (date == null)
            return "";
        String time;
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern("yyyy-MM-dd HH-mm-ss");
        time = formater.format(date);
        return time;
    }
}

Related Tutorials