Java Date Format dateformat(String dateTime)

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

Description

dateformat

License

LGPL

Declaration

public static Date dateformat(String dateTime) throws ParseException 

Method Source Code

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

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

public class Main {
    public static Date dateformat(String dateTime) throws ParseException {
        SimpleDateFormat formatter = null;
        //String dateTime = "2002-02-02T22:22:22Z";
        //String dateTime = "2002-02-02";
        int dot = dateTime.indexOf('.');
        int col = dateTime.indexOf(':');
        if (col > 0) {
            if (dot > 0) {
                formatter = new SimpleDateFormat(
                        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            } else {
                formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            }// ww w.j av  a 2 s .  com
        } else {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
        }
        //formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date dt = formatter.parse(dateTime);
        return dt;
    }
}

Related

  1. dateFormat(java.util.Date date, String formatStr)
  2. dateFormat(long time)
  3. dateFormat(String date, String dateFormat)
  4. DateFormat(String dateStr, String oldPattern, String newPattern)
  5. dateFormat(String datetime)
  6. dateFormat(String df, TimeZone timeZone)
  7. dateFormat(String format, java.util.Date date)
  8. dateFormat(String format, java.util.Date date, String tz, Locale locale)
  9. dateFormat(String format, String defaultFormat)