Social Icons

twitterfacebookgoogle pluslinkedinrss feed

Monday, March 28, 2011

How do I get dates between two dates?

In the following example code I am going to get  dates between two dates. First, I need to convert the date value into milliseconds and increase startup date value from one date.

One date in milliseconds equlas to 24 * 60 * 60 * 1000L

import java.util.*;

public class DateUtil {
 static final long ONE_DAY = 24 * 60 * 60 * 1000L;

 public static void main(String[] args) {
  getDatesBetween("03/23/2011","03/28/2011");

 }

 public static void getDatesBetween(String startDate,String endDate) {


        long  from=Date.parse(startDate);  // long value of from date

        long to=Date.parse(endDate);     // long value of to date

        int x=0;

        while(from <= to) {
              x=x+1;
              System.out.println ("Dates  : "+new Date(from));
              from += ONE_DAY;
        }
        System.out.println ("No of Dates  :"+ x);


 }


}



Java Hex Color Code Generator

Last week I got an ideas on a random color generator in Java. One of my friends asked me to implement this logic.

Here is my example hexadecimal color code generate logic.


public Map hexCodeGenerator(int colorCount){
  int maxColorValue = 16777215;
  // this is decimal value of the "FFFFFF"

  int devidedvalue = maxColorValue/colorCount;

  int countValue = 0;

  HashMap hexColorMap = new HashMap();

  for(int a=0; a < colorCount && maxColorValue >= countValue ; a++){
   if(a != 0){
    countValue+=devidedvalue;
    hexColorMap.put(a,Integer.toHexString( 0x10000 | countValue).substring(1).toUpperCase());

   }else{
    hexColorMap.put(a,Integer.toHexString( 0x10000 | countValue).substring(1).toUpperCase());
   }



  }
  return hexColorMap;

 }



Or You can use Math.random also. Here is example using Math.random

public static Map randomCodeGenerator(int colorCount){
  HashMap hexColorMap = new HashMap();
  for(int a=0;a < colorCount; a++){
   String code = ""+(int)(Math.random()*256);
   code = code+code+code;
   int  i = Integer.parseInt(code);

   
   hexColorMap.put(a,Integer.toHexString( 0x1000000 | i).substring(1).toUpperCase());
  }
 return hexColorMap;
 }


help my site stand out :