Data Time class
- Data Time class use with Android and Java.
- This class is use for get current date time.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
/*
* Created By : Punit Solanki
*/
public class DateTime
{
private static SimpleDateFormat mDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
/* Gives current mobile TIME */
public static String getTime()
{
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
String s_time = (String.valueOf(hour).length() == 1 ? "0"
+ String.valueOf(hour) : String.valueOf(hour))
+ ":"
+ (String.valueOf(min).length() == 1 ? "0"
+ String.valueOf(min) : String.valueOf(min))
+ ":"
+ (String.valueOf(sec).length() == 1 ? "0"
+ String.valueOf(sec) : String.valueOf(sec));
return s_time;
}
/* Gives current mobile DATE */
public static String getDate()
{
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.YEAR);
String s_date = (String.valueOf(date).length() == 1 ? "0"
+ String.valueOf(date) : String.valueOf(date))
+ "/"
+ (String.valueOf(month).length() == 1 ? "0"
+ String.valueOf(month) : String.valueOf(month))
+ "/"
+ (String.valueOf(year).length() == 1 ? "0"
+ String.valueOf(year) : String.valueOf(year));
return s_date;
}
/*
* Set Date Format with "/"
* Parameter : All are Integer
* 1) DAY integer
* 2) MONTH integer
* 3) YEAR integer
* Return : String
* 01/01/2014
*
*/
public static String setDateFormat(int day, int month, int year)
{
month+=1;
String dateFormat = (String.valueOf(day).length() == 1 ? "0" + String.valueOf(day) : String.valueOf(day))
+ "/" + (String.valueOf(month).length() == 1 ? "0" + String.valueOf(month) : String.valueOf(month))
+ "/" + (String.valueOf(year));
return dateFormat;
}
/*
* Get Date from full date
* Parameter : String
* 01/12/2014
* Return : String
* 01
*/
public static String getDayFromFullDate(String date)
{
return date.substring(0, 2);
}
/*
* Get Month from full date
* Parameter : String
* 01/12/2014
* Return : String
* 12
*/
public static String getMonthFromFullDate(String date)
{
return date.substring(3, 5);
}
/*
* Get Year from full date
* Parameter : String
* 01/12/2014
* Return : String
* 2014
*/
public static String getYearFromFullDate(String date)
{
return date.substring(6, date.length());
}
/*
* Set Date from YYYY/MM/DD to DD/MM/YYYY
* Parameter : String
* 2014/12/01
* Return : String
* 01/12/2014
*/
public static String setDateAsDDMMYYYY(String dateYYYYMMDD)
{
String date=dateYYYYMMDD;
date = date.substring(8, date.length())+
"/"+date.substring(5, 7)+
"/"+date.substring(0, 4);
return date;
}
/*
* Set Date from DD/MM/YYYY to YYYY/MM/DD
* Parameter : String
* 01/12/2014
* Return : String
* 2014/12/01
*/
public static String setDateAsYYYYMMDD(String dateDDMMYYYY)
{
String date=dateDDMMYYYY;
date = date.substring(6, date.length())+
"/"+date.substring(3, 5)+
"/"+date.substring(0, 2);
return date;
}
public static String getHourFromFullTime(String fullTime)
{
return fullTime.substring(0,2);
}
public static String getMinitFromFullTime(String fullTime)
{
return fullTime.substring(3,5);
}
public static String getSecondFromFullTime(String fullTime)
{
return fullTime.substring(6,fullTime.length());
}
/**
* get current date and time as String
*
* @return
*/
public static String getDateTimeString()
{
final GregorianCalendar now = new GregorianCalendar();
return mDateTimeFormat.format(now.getTime());
}
}
- This class is use for get current date time.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
/*
* Created By : Punit Solanki
*/
public class DateTime
{
private static SimpleDateFormat mDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
/* Gives current mobile TIME */
public static String getTime()
{
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
String s_time = (String.valueOf(hour).length() == 1 ? "0"
+ String.valueOf(hour) : String.valueOf(hour))
+ ":"
+ (String.valueOf(min).length() == 1 ? "0"
+ String.valueOf(min) : String.valueOf(min))
+ ":"
+ (String.valueOf(sec).length() == 1 ? "0"
+ String.valueOf(sec) : String.valueOf(sec));
return s_time;
}
/* Gives current mobile DATE */
public static String getDate()
{
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.YEAR);
String s_date = (String.valueOf(date).length() == 1 ? "0"
+ String.valueOf(date) : String.valueOf(date))
+ "/"
+ (String.valueOf(month).length() == 1 ? "0"
+ String.valueOf(month) : String.valueOf(month))
+ "/"
+ (String.valueOf(year).length() == 1 ? "0"
+ String.valueOf(year) : String.valueOf(year));
return s_date;
}
/*
* Set Date Format with "/"
* Parameter : All are Integer
* 1) DAY integer
* 2) MONTH integer
* 3) YEAR integer
* Return : String
* 01/01/2014
*
*/
public static String setDateFormat(int day, int month, int year)
{
month+=1;
String dateFormat = (String.valueOf(day).length() == 1 ? "0" + String.valueOf(day) : String.valueOf(day))
+ "/" + (String.valueOf(month).length() == 1 ? "0" + String.valueOf(month) : String.valueOf(month))
+ "/" + (String.valueOf(year));
return dateFormat;
}
/*
* Get Date from full date
* Parameter : String
* 01/12/2014
* Return : String
* 01
*/
public static String getDayFromFullDate(String date)
{
return date.substring(0, 2);
}
/*
* Get Month from full date
* Parameter : String
* 01/12/2014
* Return : String
* 12
*/
public static String getMonthFromFullDate(String date)
{
return date.substring(3, 5);
}
/*
* Get Year from full date
* Parameter : String
* 01/12/2014
* Return : String
* 2014
*/
public static String getYearFromFullDate(String date)
{
return date.substring(6, date.length());
}
/*
* Set Date from YYYY/MM/DD to DD/MM/YYYY
* Parameter : String
* 2014/12/01
* Return : String
* 01/12/2014
*/
public static String setDateAsDDMMYYYY(String dateYYYYMMDD)
{
String date=dateYYYYMMDD;
date = date.substring(8, date.length())+
"/"+date.substring(5, 7)+
"/"+date.substring(0, 4);
return date;
}
/*
* Set Date from DD/MM/YYYY to YYYY/MM/DD
* Parameter : String
* 01/12/2014
* Return : String
* 2014/12/01
*/
public static String setDateAsYYYYMMDD(String dateDDMMYYYY)
{
String date=dateDDMMYYYY;
date = date.substring(6, date.length())+
"/"+date.substring(3, 5)+
"/"+date.substring(0, 2);
return date;
}
public static String getHourFromFullTime(String fullTime)
{
return fullTime.substring(0,2);
}
public static String getMinitFromFullTime(String fullTime)
{
return fullTime.substring(3,5);
}
public static String getSecondFromFullTime(String fullTime)
{
return fullTime.substring(6,fullTime.length());
}
/**
* get current date and time as String
*
* @return
*/
public static String getDateTimeString()
{
final GregorianCalendar now = new GregorianCalendar();
return mDateTimeFormat.format(now.getTime());
}
}
Comments
Post a Comment