Java Day of formatDate(String day)

Here you can find the source of formatDate(String day)

Description

format Date

License

Open Source License

Declaration

public static String formatDate(String day) 

Method Source Code


//package com.java2s;
/*/*from  w w w .j  a  v  a 2 s  .  c o  m*/
 * Copyright 2010 Mttang.com All right reserved. This software is the
 * confidential and proprietary information of Mttang.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Mttang.com.
 */

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

public class Main {
    static final SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyy-MM");
    static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    public static String formatDate(String day) {
        try {
            return yyyyMM.format(formatYearMonth(day));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String formatDate(String day, String pattern) {
        SimpleDateFormat daydf = new SimpleDateFormat(pattern);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(parseDayByYYYYMMDD(day));
        return daydf.format(cal.getTime());
    }

    public static Date formatYearMonth(String day) {
        try {
            return yyyyMM.parse(day);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return new java.util.Date();
        }
    }

    public static String formatYearMonth(Date date) {
        try {
            return yyyyMM.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static long parseDayByYYYYMMDD(String str) {
        try {
            return yyyyMMdd.parse(str).getTime();
        } catch (Exception ex) {
            return 0L;
        }
    }
}

Related

  1. calcDay(String date1, String date2, String format)
  2. calcSunday(String queryDate)
  3. CountCmpHolidays(Date stdate, Date enddate, String[] CmpHoliDays)
  4. countDay(String begin, String end)
  5. endOfTheDay(Date date)
  6. formatDay(final Date time)
  7. formatDayTime(String day)
  8. formatShortNameOfDay(final Date date)
  9. get1DayBeforDate()