Java Date Format Guess getDateFormat(String dateStr)

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

Description

get Date Format

License

Apache License

Declaration

public static DateFormat getDateFormat(String dateStr) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

public class Main {
    private static DateFormat fmtDate = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat fmtTime = new SimpleDateFormat("HH:mm:ss");
    private static DateFormat fmtDT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static DateFormat getDateFormat(String dateStr) {
        int pos1 = dateStr.indexOf("-");
        int pos2 = dateStr.indexOf(":");
        DateFormat dt = null;/*  w  w w .j  a  v a  2 s  .  c om*/
        if (pos1 != -1 && pos2 != -1) {
            dt = fmtDT;
        } else if (pos1 != -1 && pos2 == -1) {
            dt = fmtDate;
        } else if (pos1 == -1 && pos2 != -1) {
            dt = fmtTime;
        } else {
            dt = fmtDate;
        }
        return dt;
    }
}

Related

  1. getDateFormat(Date date)
  2. getDateFormat(Date date, String format)
  3. getDateFormat(String date)
  4. getDateFormat(String date)
  5. getDateFormat(String date)
  6. getDateFormatted(Date date)