package com.hinduja.util;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.DateField;
public class DateUtility {
static Calendar calendar;
static {
calendar = Calendar.getInstance();
calendar.setTime(new Date());
}
public static Calendar setDate(String date){
return calendar;
}
public static String getDate(){
Date today = new Date(System.currentTimeMillis());
DateField date = new DateField("", DateField.DATE_TIME);
date.setDate(today);
Calendar cal = Calendar.getInstance();
cal.setTime(today);
String dateStr = "" + getTwoDigitStr(cal.get(Calendar.DATE)) + "." +
getTwoDigitStr(cal.get(Calendar.MONTH)+1) + "." +
cal.get(Calendar.YEAR) + " " +
getTwoDigitStr(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
getTwoDigitStr(cal.get(Calendar.MINUTE)) + ":" +
getTwoDigitStr(cal.get(Calendar.SECOND));
return dateStr;
}
public static String convertDate(Date fromDate){
Date today = fromDate;
DateField date = new DateField("", DateField.DATE_TIME);
date.setDate(today);
Calendar cal = Calendar.getInstance();
cal.setTime(today);
String toDate = "" + getTwoDigitStr(cal.get(Calendar.DATE)) + "." +
getTwoDigitStr(cal.get(Calendar.MONTH)+1) + "." +
cal.get(Calendar.YEAR) + " " +
getTwoDigitStr(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
getTwoDigitStr(cal.get(Calendar.MINUTE)) + ":" +
getTwoDigitStr(cal.get(Calendar.SECOND));
return toDate;
}
public static String getTwoDigitStr (int number) {
if (number < 10) {
return ("0" + number);
}
else {
return String.valueOf(number);
}
}
public static int getDay() {
return calendar.get(Calendar.DATE);
}
public static int getMonth() {
return calendar.get(Calendar.MONTH)+1;
}
public static int getYear() {
return calendar.get(Calendar.YEAR);
}
public static int compareDate (String dateStr) {
Calendar readingCal = null;
try {
readingCal = parseDate(dateStr);
}
catch (Exception e) {
return 12;
}
Calendar currCal = Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
currCal.setTime(date);
int readingMonths = readingCal.get(Calendar.YEAR) * 12 + readingCal.get(Calendar.MONTH);
int currMonths = currCal.get(Calendar.YEAR) * 12 + currCal.get(Calendar.MONTH);
return currMonths - readingMonths;
}
public static boolean to_from_date_compare(String to_date,String from_date,String db_date)
{
int diff_year;
int diff_month;
int to_date_day = Integer.parseInt(to_date.substring(0,2));
int from_date_day = Integer.parseInt(from_date.substring(0,2));
int db_date_day = Integer.parseInt(db_date.substring(0,2));
int to_date_month = Integer.parseInt(to_date.substring(3,5));
int from_date_month = Integer.parseInt(from_date.substring(3,5));
int db_date_month = Integer.parseInt(db_date.substring(3,5));
int to_date_year = Integer.parseInt(to_date.substring(6,10));
int from_date_year = Integer.parseInt(from_date.substring(6,10));
int db_date_year = Integer.parseInt(db_date.substring(6,10));
if((to_date_year <= db_date_year) && (db_date_year <= from_date_year))
{
if((to_date_month <= db_date_month) && (db_date_month <= from_date_month))
{
if((to_date_day <= db_date_day) && (db_date_day <= from_date_day))
{
return true;
}
else
{
if(((db_date_day > from_date_day) && (db_date_month >= from_date_month) && (db_date_year >= from_date_year)) || ((db_date_day <= to_date_day) && (db_date_month <= to_date_month) && db_date_year <= to_date_year))
{
return false;
}
diff_month = from_date_month - to_date_month;
diff_year = from_date_year - to_date_year;
if(diff_month > 0 || diff_year > 0)
return true;
}
return false;
}
else
{
if(((db_date_month >= from_date_month) && (db_date_year >= from_date_year)) || ((db_date_month <= to_date_month) && (db_date_year <= to_date_year)))
{
return false;
}
diff_year = from_date_year - to_date_year;
if(diff_year > 0)
return true;
}
return false;
}
return false;
}
public static boolean validateDate(String to_date,String from_date)
{
int to_date_day = Integer.parseInt(to_date.substring(0,2));
int from_date_day = Integer.parseInt(from_date.substring(0,2));
int to_date_month = Integer.parseInt(to_date.substring(3,5));
int from_date_month = Integer.parseInt(from_date.substring(3,5));
int to_date_year = Integer.parseInt(to_date.substring(6,10));
int from_date_year = Integer.parseInt(from_date.substring(6,10));
if(to_date_year < from_date_year)
{
return false;
}
else
{
if(to_date_month < from_date_month)
{
return false;
}
else
{
if(to_date_day < from_date_day)
return false;
}
}
return true;
}
/*
Accepted Format:
01234567890123456789012
"YYYY MM DD HH mm ss SSS"
ex. 1960-07-31 00:00:00.0
Assumptions:
- there is at exactly 1 character (any character) between each field
(which will be ignored during parsing)
*/
public static Calendar parseDate(String dateString) {
Date date = new Date(0);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (dateString == null || dateString.equals("")) {
throw new IllegalArgumentException(
"Invalid String to Parse as Date - dateString was null or empty");
}
int strSize = dateString.length();
if (strSize < 16) {
throw new IllegalArgumentException(
"Invalid String to Parse as Date - dateString invalid string length ("+strSize+")");
}
String dayStr = dateString.substring(0,2);
String monthStr = dateString.substring(3,5);
String yearStr = dateString.substring(6,10);
String hourStr = dateString.substring(11,13);
String minuteStr = dateString.substring(14,16);
String secondsStr = dateString.substring(17,19);
int year = 0;
int day = 0;
int month = 0;
int hour = 0;
int minute = 0;
int seconds = 0;
try {
year = Integer.parseInt(yearStr);
day = Integer.parseInt(dayStr);
month = Integer.parseInt(monthStr) - 1; //Zero Based Months
hour = Integer.parseInt(hourStr);
minute = Integer.parseInt(minuteStr);
seconds = Integer.parseInt(secondsStr);
}
catch (Exception e) {}
cal.set(Calendar.MONTH,month);
cal.set(Calendar.DATE,day);
cal.set(Calendar.YEAR,year);
cal.set(Calendar.HOUR_OF_DAY,hour);
cal.set(Calendar.MINUTE,minute);
cal.set(Calendar.SECOND,seconds);
return cal;
}
public static int compareTime (String dateStr1, String dateStr2) {
long differance = timeDifferance(dateStr1, dateStr2);
if (differance > 0) {
return 1;
}
else if (differance < 0) {
return -1;
}
else {
return 0;
}
}
public static long timeDifferance (String dateStr1, String dateStr2) {
Calendar cal1 = parseDate(dateStr1);
Calendar cal2 = parseDate(dateStr2);
return cal1.getTime().getTime() - cal2.getTime().getTime();
}
public static long millsPassed (String dateStr) {
Calendar cal = parseDate(dateStr);
return System.currentTimeMillis() - cal.getTime().getTime();
}
public static void main(String[] args) {
System.out.println("Long Data = "+System.currentTimeMillis());
//long threeMonthMills = 24L*60L*60L*1000L;
//long millsPassed = millsPassed("12.03.2010 08:10:00");
//System.out.println("Mills passed: " + (millsPassed > threeMonthMills) + " : " + threeMonthMills);
//System.out.println(">> " + millsPassed/(60*60*1000));
/* String dateStr = "2009-12-31 00:00:00.0";
//System.out.println(">> " + compareDate("12.03.2010 08:21:45"));
//System.out.println("## " + compareTime("12.03.2010 08:21:45", "12.03.2010 08:21:45"));
//System.out.println("Current date: " + getDate());
*/ }
}
No comments:
Post a Comment