Sunday, 28 June 2015

How to calculate difference of date and time in Java


To calculate date and time difference with manual calculation...

  • Very first step to convert date into milliseconds (ms)
    • String date1 = "01-02-2015 09:15:20";
    • SimpleDateFormat format = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      • HH convert into 24 hours format (0-23)
      • hh convert into 12 hours format
    •  Date d1 = format.parse(date1);
    • long ms = d1.getTime();


/*
1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day
*/
package com.jpt.dateExample;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDiffExample {
public static void main(String[] args) {
String dateStart = "02/14/2015 09:29:58";
String dateStop = "02/15/2015 10:31:48";
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date jd1 = null;
Date jd2 = null;
try {
jd1 = format.parse(dateStart);
jd2 = format.parse(dateStop);
//in milliseconds
long diff = jd2.getTime() - jd1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Thursday, 25 June 2015

Angular JS Directice in IE8


  • The most powerful feature Directive in Angular JS. 
  • Allow you to create custom reusable components. 
  • Few exception are there...keep in mind..!!!
  • With IE8 Angular Js should work consistent like other browser.
  • Four ways to create directive
    1. Angular JS viz. Attribute  (Common One)
    2. Element (Common One)
    3. Class
    4. Comment
  •  Let take one example :
    • Your directive is like : <div my-js-control>....</div>
    • Now use this directive in IE8
    • Copy following syntax and paste it in your HTML/JSP page.
<Html>
    <head>

   <!--[if lte IE 8]>
      <script>
        document.createElement('div-my-js-control');
      </script>
    <![endif]-->

   </head>
</html>


  • This document.createElement() creates an element called 'my-js-control' which can use in application.
  • If you create more than one directive in your system then you need to create that many new elements for older browser
  • Long story short : It is preferable to use Angular JS directive in IE8 as attributes.