Java Day of Week getDayWeekDate(String dateStr)

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

Description

get Day Week Date

License

Open Source License

Declaration

public static Date getDayWeekDate(String dateStr) throws ParseException 

Method Source Code

//package com.java2s;
/**//from  w  w  w . j  ava2s  . co m
* License: https://github.com/votingsystem/votingsystem/wiki/Licencia
*/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    private static final DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
            Locale.getDefault());

    public static Date getDayWeekDate(String dateStr) throws ParseException {
        try {
            return getDateFromString(dateStr, "dd MMM yyyy' 'HH:mm");
        } catch (Exception ex) {
            Calendar resultCalendar = Calendar.getInstance();
            resultCalendar.setTime(getDateFromString(dateStr, "EEE dd MMM' 'HH:mm"));
            resultCalendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
            return resultCalendar.getTime();
        }
    }

    public static Date getDateFromString(String dateString) throws ParseException {
        if (dateString.endsWith("Z"))
            return isoDateFormat.parse(dateString);
        else {
            DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            return formatter.parse(dateString);
        }
    }

    public static Date getDateFromString(String dateString, Locale locale) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale);
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        return formatter.parse(dateString);
    }

    public static Date getDateFromString(String dateString, String format) throws ParseException {
        DateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(dateString);
    }
}

Related

  1. getDayOfWeekAsString(String sYear, String sMonth, String sDay)
  2. getDayofWeekByDay(String year, String month, String day)
  3. getDayofWeekForContainedDate(Date dt, DayOfWeek dow)
  4. getDaysByWeek(Integer month, Integer year, Integer week)
  5. getDaysByWeek2(Integer month, Integer year, Integer begin)
  6. getDayWeekDateStr(Date date)
  7. getEndOfWeek(Date date)
  8. getFirstDayOfLastWeek(Date date)
  9. getFirstDayOfWeek(int year, int week)