/** * */ package com.kant.calendertest; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; /** * @author shaskant * */ public class Mycalender { /** * http://www.java2s.com/Tutorial/Java/ * 0120__Development/FulldaynameSimpleDateFormatEEEE.htm * * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { Date date = parseDate("Wed dec 24 20:29:30 JST 2016"); System.out.println("This Week: " + getThisWeek(date).toString()); ; System.out.println("This Month: " + getThisMonth(date).toString()); } /** * Parse date from target string * * @param target * @return * @throws ParseException */ private static Date parseDate(String target) throws ParseException { DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); Date result = df.parse(target); System.out.println(result); return result; } /** * Get month start and end dates * * @param date * @return */ private static DateRange getThisMonth(Date date) { Calendar cal = GregorianCalendar.getInstance(); cal.setTime(date); cal.set(Calendar.DATE, cal.getActualMinimum(Calendar.DATE)); Date startDate = cal.getTime(); cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); Date endDate = cal.getTime(); return new DateRange(startDate, endDate); } /** * @return * */ private static DateRange getThisWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_WEEK, 2); Date startDate = cal.getTime(); cal.add(Calendar.DAY_OF_WEEK, 6); // adds 6 days to initial day set. Date endDate = cal.getTime(); return new DateRange(startDate, endDate); } /** * Placeholder to return two date values.
* * @author shaskant * */ static class DateRange { private Date startDate = null; private Date endDate = null; /** * @param startDate * @param endDate */ public DateRange(Date startDate, Date endDate) { super(); this.startDate = startDate; this.endDate = endDate; } /** * @return the startDate */ public Date getStartDate() { return startDate; } /** * @return the endDate */ public Date getEndDate() { return endDate; } @Override public String toString() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd EEEE"); return "Starts on: " + formatter.format(startDate) + " Ends on: " + formatter.format(endDate); } } }
Comments
Post a Comment