package com.techiehints.utils;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TechiehintsDateUtils 
 
 /**
 * Calculates the Date, fromDate to next no of days
 *
 * @param fromDate - defines from date
 * @param day - next no Of days from date 
 * @return added date to the fromDate
 */
 public static Date addDays(Date fromDate, int days)
 
    Calendar cal = Calendar.getInstance();
    cal.setTime(fromDate);
    cal.add(Calendar.DATE, days); //minus number would decrement the days
    
    return new Date(cal.getTime().getTime()); 
 
 
 /**
 * Calculates the Week Start Date and Week End Date
 *
 * @param date any random date 
 * @return map with key's weekStart & weekEnd 
 * eg: weekStart=2017-04-17, weekEnd=2017-04-23
 */
 public static Map<String, Date> getWeekStartEndDate ( Date date ) 
 
     Map<String, Date> map = new HashMap<String, Date>();
 
     Calendar currentDate = Calendar.getInstance();
     currentDate.setTime(date);
     currentDate.setFirstDayOfWeek(Calendar.MONDAY);
     int firstDayOfWeek = currentDate.getFirstDayOfWeek();
     System.out.println(" Current Date " + date );
 
     Calendar startDate = Calendar.getInstance();
     startDate.setTime(currentDate.getTime());
 
    //while (startDate.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek) 
      // startDate.add(Calendar.DATE, -1);
    //
    int days = (startDate.get(Calendar.DAY_OF_WEEK) + 7 - firstDayOfWeek) % 7;
    startDate.add(Calendar.DATE, -days);
    System.out.println(" Week Start Date " + startDate.getTime());
 
    Date date1 = getSQLDate ( startDate.getTime() ); 
    map.put("weekStart", date1);
 
    Calendar endDate = Calendar.getInstance();
    endDate.setTime(startDate.getTime());
    endDate.add(Calendar.DATE, 6);
 
    date1 = getSQLDate ( endDate.getTime() );
    map.put("weekEnd", date1);
 
    System.out.println(" Week End Date " + endDate.getTime());
 
    return map;
 
 
 
 /**
 * Parse the util date to sql date 
 *
 * @param java.util.Date date any random date 
 * @return java.sql.Date date
 * 
 */
 public static Date getSQLDate(java.util.Date date) 
    Date sqlDate = new Date(date.getTime());
    return sqlDate; 
 
 
 /**
 * Parse the string date in MMddyyyyhhmm date to sql Timestamp 
 *
 * @param String MMddyyyyhhmm any random date 
 * @return java.sql.Date date
 * 
 */
 public static Timestamp getTimestamp(String MMddyyyyhhmm) throws ParseException 

   DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
   java.util.Date date = dateFormat.parse(MMddyyyyhhmm);
   long time = date.getTime();

   return new Timestamp(time);
 
 
 /**
 * Parse the util date to sql date 
 *
 * @param java.util.Date date any random fromDate 
 * @param java.util.Date date any random tillDate 
 * 
 * @return java.sql.Date date
 * 
 */
 public static List<Date> getDaysBetweenDates(Date fromDate, Date tillDate)
 
     List<Date> dates = new ArrayList<Date>();
     Calendar calendar = Calendar.getInstance();
     calendar.setTime(fromDate);

     while (calendar.getTime().before(tillDate))
     
       Date result = getSQLDate(calendar.getTime());
       dates.add(result);
       calendar.add(Calendar.DATE, 1);
     
     return dates;
 
 
 public static void main(String[] args) 
 
    Date date = addDays( new Date(new java.util.Date().getTime()), 15);
    System.out.println(date);
    System.out.println(getWeekStartEndDate(date));
 
    System.out.println( getDaysBetweenDates ( addDays( new Date(new java.util.Date().getTime()), 1), date ));
 
 
Advertisements