This document describes how the Date Conversion and RGB Color conversion can be done in Java
public
class
DateUtil
{
public
static Date stringToDate(String dateStr) throws ParseException
{
SimpleDateFormat
dateFormatter = new
SimpleDateFormat("yyyy-MM-dd");
Date
date = null;
if(dateStr
!= null)
{
date
= dateFormatter.parse(dateStr);
}
return
date;
}
public
static String
dateToString(Date date)
{
return
dateToString(date, "yyyy-MM-dd");
}
public
static String
dateToStringMMM(Date date)
{
return
dateToString(date, "dd-MMM-yyyy");
}
public
static String
dateTimeToString(Date date)
{
return
dateToString(date, "yyyy-MM-dd
HH:mm:ss");
}
public
static String
dateToString(Date date, String pattern)
{
SimpleDateFormat
dateFormatter = new
SimpleDateFormat(pattern);
String
dateStr = null;
if(date
!= null)
{
dateStr
= dateFormatter.format(date);
}
return
dateStr;
}
public
static Date
stringMMMToDate(String dateStr) throws
ParseException
{
SimpleDateFormat
dateFormatter = new
SimpleDateFormat("dd-MMM-yyyy");
Date
date = null;
if(dateStr
!= null)
{
date
= dateFormatter.parse(dateStr);
}
return
date;
}
public
static Date
findPreviousMonday(Calendar calendar)
{
switch(calendar.get(Calendar.DAY_OF_WEEK))
{
case
Calendar.TUESDAY:
calendar.add(Calendar.DATE,
-1);
break;
case
Calendar.WEDNESDAY:
calendar.add(Calendar.DATE,
-2);
break;
case
Calendar.THURSDAY:
calendar.add(Calendar.DATE,
-3);
break;
case
Calendar.FRIDAY:
calendar.add(Calendar.DATE,
-4);
break;
case
Calendar.SATURDAY:
calendar.add(Calendar.DATE,
-5);
break;
case
Calendar.SUNDAY:
calendar.add(Calendar.DATE,
-6);
}
return
calendar.getTime();
}
}
RGB Color Utility in Java
This section describes how RGB Color can be manipulated in Java
public static String
intToHexString(int r1,
int g1,
int b1)
{
String ret = null;
if(r1>= 0 && r1 <= 255 &&
g1 >= 0 && g1 <= 255 &&
b1 >= 0 && b1 <= 255)
{
String red =Integer.toHexString(r1).toUpperCase();
String green =Integer.toHexString(g1).toUpperCase();
String blue =Integer.toHexString(b1).toUpperCase();
if(red.length() == 1)
{
red = "0"+red;
}
if (green.length() == 1)
{
green = "0"+green;
}
if(blue.length() == 1)
{
blue = "0"+blue;
}
ret = red+green+blue;
}
return ret;
}
public static int[] hexStringToInt(String color)
{
int[]
ret = new int[3];
if(color !=
null && color.length() == 6)
{
String red,green,blue;
red=color.substring(0,2);
green=color.substring(2,4);
blue=color.substring(4,6);
ret[0] = Integer.parseInt(red, 16);
ret[1] = Integer.parseInt(green, 16);
ret[2] = Integer.parseInt(blue, 16);
}
return ret;
}