Java Date Format Change convertDatetimeFormat(Date tar_date, String date_format)

Here you can find the source of convertDatetimeFormat(Date tar_date, String date_format)

Description

convert Datetime Format

License

Open Source License

Declaration

public static String convertDatetimeFormat(Date tar_date, String date_format) 

Method Source Code


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

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

public class Main {
    public static String convertDatetimeFormat(Date tar_date, String date_format) {

        SimpleDateFormat sdf = new SimpleDateFormat(date_format);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        String formated_date = sdf.format(tar_date);

        //////////////////////////////////////////////////////////
        // Before: 2012-07-23T02:33:09+0000
        // After:  2012-07-23T02:33:09+00:00
        char last_char = date_format.charAt(date_format.length() - 1);

        if (last_char == 'Z') {
            formated_date = formated_date.substring(0, formated_date.length() - 2) + ':'
                    + formated_date.substring(formated_date.length() - 2, formated_date.length());
        }//from   w w  w  .  j  a v a2s  . c om
        //////////////////////////////////////////////////////////

        return formated_date;
    }
}

Related

  1. convertDateFormatByRawOffset(DateFormat formatter, int rawOffset, long milliSecond)
  2. convertDateFromInDayFormat(String stringDate, String hhmmss)
  3. convertDateFromTo(String stringDate, String patternToUse, TimeZone timezoneIn, TimeZone timezoneOut)
  4. convertDateStringToString(String dateString, String format)
  5. convertDateStringToString(String strDate, String currentFormat, String parseFormat)
  6. convertIso8601FormatToArsTime(String timeString)
  7. convertIso8601FormatToArsTimeOfDay(String timeOfDayString)
  8. convertISO8610ToCalendar(String iso8601string)
  9. convertTimeStringToTimestamp(String timeString, String timeFormat)