Java UTC Date utcTimeToString(String dateStr)

Here you can find the source of utcTimeToString(String dateStr)

Description

utc Time To String

License

Open Source License

Declaration

public static String utcTimeToString(String dateStr) 

Method Source Code

//package com.java2s;
/**//from  w ww  .jav  a 2 s.  c  om
 * JWatch - Quartz Monitor: http://code.google.com/p/jwatch/
 * Copyright (C) 2011 Roy Russo and the original author or authors.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 **/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.common.base.Strings;

public class Main {
    public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    public static String utcTimeToString(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DEFAULT);
        String time = "";
        try {
            Date date = format.parse(dateStr);
            time = toStringFromDate(date, null);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String toStringFromDate(Date date, String format) {
        try {
            if (Strings.isNullOrEmpty(format)) {
                format = DATE_FORMAT_DEFAULT;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. toUTCDate(String dateStr)
  2. toUTCString(Date date)
  3. toUTCString(long t)
  4. utcTime(Locale locale)
  5. utcTimestamp(Date date)
  6. utcToDate(String utcTimestamp)
  7. utcToLocal(Date date)
  8. utcTolocal(String utc)