Thursday, March 1, 2018

DateUtility


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

import javax.swing.text.DateFormatter;

public class DateUtility {

 private static final String YYYY_MM_DD_HH_MM_SS_Z = "yyyy-MM-dd HH:mm:ss z";

 public static void main(String[] args) {

  updateJulyDates();
  System.out.println();
  updateFebrurayDates();
  System.out.println();
  System.out.println();

  updateSampleOrderDates();

 }

 public static void updateSampleOrderDates() {

  LocalDateTime DOO = LocalDateTime.of(2018, Month.JULY, 8, 4, 10, 10);// SUNDAY
  LocalDateTime ESD = LocalDateTime.of(2018, Month.JULY, 12, 4, 10, 10);// THURSDAY
  LocalDateTime LDD = LocalDateTime.of(2018, Month.JULY, 16, 4, 10, 10);// MONDAY
  LocalDateTime SBD = LocalDateTime.of(2018, Month.JULY, 10, 4, 10, 10);// TUESDAY
  LocalDateTime RBD = LocalDateTime.of(2018, Month.JULY, 12, 4, 10, 10);// THURSDAY

  System.out.println(" old DOO : " + DOO.format(DateTimeFormatter.ISO_DATE) + " 
   DOW :  " + DOO.getDayOfWeek()
    + "     new DOO :  " + updateDate(DOO).format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + updateDate(DOO).getDayOfWeek());
  System.out.println(" old ESD : " + ESD.format(DateTimeFormatter.ISO_DATE) + " 
   DOW :  " + ESD.getDayOfWeek()
    + "   new ESD:  " + updateDate(ESD).format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + updateDate(ESD).getDayOfWeek());
  System.out.println(" old LDD: " + LDD.format(DateTimeFormatter.ISO_DATE) + "     DOW :  " + LDD.getDayOfWeek()
    + "     new LDD:  " + updateDate(LDD).format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + updateDate(LDD).getDayOfWeek());
  System.out.println(" old SBD: " + SBD.format(DateTimeFormatter.ISO_DATE) + "
     DOW :  " + SBD.getDayOfWeek()
    + "    new SBD:  " + updateDate(SBD).format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + updateDate(SBD).getDayOfWeek());
  System.out.println(" old RBD: " + RBD.format(DateTimeFormatter.ISO_DATE) + "     DOW :  " + RBD.getDayOfWeek()
    + "   new RBD:  " + updateDate(RBD).format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + updateDate(RBD).getDayOfWeek());

 }

 public static void updateJulyDates() {

  LocalDateTime july_2011 = LocalDateTime.of(2011, Month.JULY, 10, 4, 10, 10);
  LocalDateTime july_2012 = LocalDateTime.of(2012, Month.JULY, 8, 4, 10, 10);
  LocalDateTime july_2013 = LocalDateTime.of(2013, Month.JULY, 7, 4, 10, 10);
  LocalDateTime july_2014 = LocalDateTime.of(2014, Month.JULY, 6, 4, 10, 10);
  LocalDateTime july_2015 = LocalDateTime.of(2015, Month.JULY, 5, 4, 10, 10);
  LocalDateTime july_2016 = LocalDateTime.of(2016, Month.JULY, 10, 4, 10, 10);
  LocalDateTime july_2017 = LocalDateTime.of(2017, Month.JULY, 9, 4, 10, 10);
  LocalDateTime july_2018 = LocalDateTime.of(2018, Month.JULY, 8, 4, 10);

  System.out.println(" old : " + july_2011.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2011.getDayOfWeek() + "    new :  " + updateDate(july_2011).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2011).getDayOfWeek());
  System.out.println(" old : " + july_2012.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2012.getDayOfWeek() + "    new :  " + updateDate(july_2012).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2012).getDayOfWeek());
  System.out.println(" old : " + july_2013.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2013.getDayOfWeek() + "    new :  " + updateDate(july_2013).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2013).getDayOfWeek());
  System.out.println(" old : " + july_2014.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2014.getDayOfWeek() + "    new :  " + updateDate(july_2014).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2014).getDayOfWeek());
  System.out.println(" old : " + july_2015.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2015.getDayOfWeek() + "    new :  " + updateDate(july_2015).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2015).getDayOfWeek());
  System.out.println(" old : " + july_2016.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2016.getDayOfWeek() + "    new :  " + updateDate(july_2016).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2016).getDayOfWeek());
  System.out.println(" old : " + july_2017.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2017.getDayOfWeek() + "    new :  " + updateDate(july_2017).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2017).getDayOfWeek());
  System.out.println(" old : " + july_2018.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + july_2018.getDayOfWeek() + "    new :  " + updateDate(july_2018).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(july_2018).getDayOfWeek());

 }

 public static void updateFebrurayDates() {

  LocalDateTime feb_2011 = LocalDateTime.of(2011, Month.FEBRUARY, 16, 4, 10, 10);
  LocalDateTime feb_2012 = LocalDateTime.of(2012, Month.FEBRUARY, 15, 4, 10, 10);
  LocalDateTime feb_2013 = LocalDateTime.of(2013, Month.FEBRUARY, 13, 4, 10, 10);
  LocalDateTime feb_2014 = LocalDateTime.of(2014, Month.FEBRUARY, 12, 4, 10, 10);
  LocalDateTime feb_2015 = LocalDateTime.of(2015, Month.FEBRUARY, 11, 4, 10, 10);
  LocalDateTime feb_2016 = LocalDateTime.of(2016, Month.FEBRUARY, 10, 4, 10, 10);
  LocalDateTime feb_2017 = LocalDateTime.of(2017, Month.FEBRUARY, 15, 4, 10, 10);
  LocalDateTime feb_2018 = LocalDateTime.of(2018, Month.FEBRUARY, 14, 4, 10);
  System.out.println();
  System.out.println();

  System.out.println(" old : " + feb_2011.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2011.getDayOfWeek() + "    new :  " + updateDate(feb_2011).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2011).getDayOfWeek());
  System.out.println(" old : " + feb_2012.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2012.getDayOfWeek() + "    new :  " + updateDate(feb_2012).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2012).getDayOfWeek());
  System.out.println(" old : " + feb_2013.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2013.getDayOfWeek() + "    new :  " + updateDate(feb_2013).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2013).getDayOfWeek());
  System.out.println(" old : " + feb_2014.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2014.getDayOfWeek() + "    new :  " + updateDate(feb_2014).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2014).getDayOfWeek());
  System.out.println(" old : " + feb_2015.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2015.getDayOfWeek() + "    new :  " + updateDate(feb_2015).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2015).getDayOfWeek());
  System.out.println(" old : " + feb_2016.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2016.getDayOfWeek() + "    new :  " + updateDate(feb_2016).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2016).getDayOfWeek());
  System.out.println(" old : " + feb_2017.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2017.getDayOfWeek() + "    new :   " + updateDate(feb_2017).format(DateTimeFormatter.ISO_DATE)
    + "    DOW :  " + updateDate(feb_2017).getDayOfWeek());
  System.out.println(" old : " + feb_2018.format(DateTimeFormatter.ISO_DATE) + "     DOW :  "
    + feb_2018.getDayOfWeek() + "    new :  " + updateDate(feb_2018).format(DateTimeFormatter.ISO_DATE)
    + "     DOW :  " + updateDate(feb_2018).getDayOfWeek());

 }

 public static LocalDateTime updateDate(LocalDateTime oldOdrDte) {

  int yearDiff = LocalDate.now().getYear() - oldOdrDte.getYear();
  int temp = yearDiff;
  if (yearDiff > 7)
   yearDiff = yearDiff % 7;
  if (temp > 7) {
   return (yearDiff < 4) ? oldOdrDte.plusDays((yearDiff * 365 - yearDiff) + (temp - yearDiff) * 365)
     : oldOdrDte.plusDays((yearDiff * 365 - (7 - yearDiff)) + (temp - yearDiff) * 365);

  } else {
   return oldOdrDte.plusDays((yearDiff < 4) ? (yearDiff * 365 - yearDiff) : (yearDiff * 365 + (7 - yearDiff)));
  }

 }

 public static LocalDateTime updateDate12(LocalDateTime oldOdrDte) {

  int yearDiff = LocalDate.now().getYear() - oldOdrDte.getYear();
  int temp = yearDiff;
  if (yearDiff > 7)
   yearDiff = yearDiff % 7;
  return oldOdrDte.plusDays((yearDiff < 4) ? (yearDiff * 365 - yearDiff) : (yearDiff * 365 + (7 - yearDiff)));
 }

}



No comments:

Post a Comment