Java Hour Format formatStringTime(String dateTime)

Here you can find the source of formatStringTime(String dateTime)

Description

format String Time

License

Open Source License

Declaration

public static String formatStringTime(String dateTime) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static String formatStringTime(String dateTime) {
        if (dateTime == null || dateTime.equals("")) {
            return null;
        }/*w  ww . j a  v  a 2s. c o  m*/
        String formatToUTC = null;
        try {
            long parseLong = Long.parseLong(dateTime);
            formatToUTC = formatToUTC(parseLong);
        } catch (NumberFormatException e2) {
            formatToUTC = null;
        }

        if (null != formatToUTC) {
            return formatToUTC;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateTime);
        } catch (ParseException e) {
            sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e1) {
                return null;
            }

        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
        return formatter.format(date);
    }

    public static String formatToUTC(long datetime) {
        return formatLongTime(datetime, "yyyyMMdd'T'HHmmss'Z'");
    }

    public static String formatLongTime(long datetime, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(new Date(datetime));
    }
}

Related

  1. formatSimpleTimeAndDateString(Date date)
  2. formatSingleFractionHours(Double updatedHours)
  3. formatSQL(Date date)
  4. formatSqlDateTime(Date value)
  5. formatSSLValid(Date validFrom, Date validTo)
  6. formatStringTimeToLong(String timeLine)
  7. formatStrToDate(String strDate, int format)
  8. formatStrToDate(String value, String pattern)
  9. formattedDate()