Java Hour Format formatDateTime(String strDateTime, String strFormat)

Here you can find the source of formatDateTime(String strDateTime, String strFormat)

Description

format Date Time

License

Apache License

Declaration

public static String formatDateTime(String strDateTime, String strFormat) 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    public static String formatDateTime(String strDateTime, String strFormat) {
        String sDateTime = strDateTime;
        try {/*from w  ww  .j  av a2  s . c o  m*/
            Calendar Cal = parseDateTime(strDateTime);
            SimpleDateFormat sdf = null;
            sdf = new SimpleDateFormat(strFormat);
            sDateTime = sdf.format(Cal.getTime());
        } catch (Exception e) {

        }
        return sDateTime;
    }

    public static Calendar parseDateTime(String baseDate) {
        Calendar cal = null;
        cal = new GregorianCalendar();
        int yy = Integer.parseInt(baseDate.substring(0, 4));
        int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
        int dd = Integer.parseInt(baseDate.substring(8, 10));
        int hh = 0;
        int mi = 0;
        int ss = 0;
        if (baseDate.length() > 12) {
            hh = Integer.parseInt(baseDate.substring(11, 13));
            mi = Integer.parseInt(baseDate.substring(14, 16));
            ss = Integer.parseInt(baseDate.substring(17, 19));
        }
        cal.set(yy, mm, dd, hh, mi, ss);
        return cal;
    }
}

Related

  1. formatDateTime(java.util.Date date)
  2. formatDateTime(java.util.Date paramDate)
  3. formatDateTime(String date)
  4. FormatDateTime(String dateString)
  5. formatDateTime(String dateTime)
  6. formatDateTime12(String dateStr)
  7. formatDateTime24()
  8. formatDateTimeForSolr(Date d)
  9. formatDateTimeFull(Date dateTime)