Java Date Convert dateToString(final Date date)

Here you can find the source of dateToString(final Date date)

Description

date To String

License

Apache License

Declaration

public static String dateToString(final Date date) 

Method Source Code


//package com.java2s;
/*/*  w  ww.j  a v a 2s.  c  o m*/
 * Copyright 2002-2016 The Jamocha Team
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.jamocha.org/
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the License.
 */

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

public class Main {
    public static String dateToString(final Date date) {
        final StringBuilder res = new StringBuilder();
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        res.append(cal.get(Calendar.YEAR)).append("-");
        final int month = cal.get(Calendar.MONTH) + 1;
        res.append((month < 10) ? "0" + month : month).append("-");
        final int day = cal.get(Calendar.DAY_OF_MONTH);
        res.append((day < 10) ? "0" + day : day).append(" ");
        final int hour = cal.get(Calendar.HOUR_OF_DAY);
        res.append((hour < 10) ? "0" + hour : hour).append(":");
        final int minute = cal.get(Calendar.MINUTE);
        res.append((minute < 10) ? "0" + minute : minute).append(":");
        final int second = cal.get(Calendar.SECOND);
        res.append((second < 10) ? "0" + second : second);
        res.append(cal.get(Calendar.ZONE_OFFSET));
        return res.toString();
    }
}

Related

  1. dateToRRNDate(Date date)
  2. DateToStr(java.util.Date val)
  3. dateToString(Date date)
  4. dateToString(Date date)
  5. dateToString(Date date)
  6. dateToUnixTimestamp(Date date)
  7. dateToUTC(Date d)
  8. dateToXdsDateString(Date date, int format)
  9. dateToXmlDateTime(final Date date)