http://www.wilsonmar.com/datepgms.htm
Interesting link
Some java source code for working with a calendar
>> Monday, September 13, 2004 4:54:20 PM - Comments by satya
Calendar cal = Calendar.getInstance(); Date today = cal.getTime(); //2 days ago's date Date 2daysAgo = cal.add(Calendar.DATE, -2).getTime(); //2 years ago cal.add(Calendar.YEAR, -2);
Satya - Monday, November 15, 2004 5:27:12 PM
Some sample date formatting code
public class TestLocale {
public static void main(String[] args)
{
Locale usEnglish = new Locale("en","US");
Locale usHindi = new Locale("hi","US");
Locale usJapanese = new Locale("ja","US");
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
DateFormat usEnglishDf = DateFormat.getDateInstance(DateFormat.SHORT,usEnglish);
DateFormat usEnglishDfLong = DateFormat.getDateInstance(DateFormat.LONG,usEnglish);
String usEnglishDate = usEnglishDf.format(today);
String usEnglishDateLong = usEnglishDfLong.format(today);
DateFormat usHindiDf = DateFormat.getDateInstance(DateFormat.SHORT,usHindi);
DateFormat usHindiDfLong = DateFormat.getDateInstance(DateFormat.LONG,usHindi);
String usHindiDate = usHindiDf.format(today);
String usHindiDateLong = usHindiDfLong.format(today);
DateFormat usJapaneseDf = DateFormat.getDateInstance(DateFormat.SHORT,usJapanese);
DateFormat usJapaneseDfLong = DateFormat.getDateInstance(DateFormat.LONG,usJapanese);
String usJapaneseDate = usJapaneseDf.format(today);
String usJapaneseDateLong = usJapaneseDfLong.format(today);
System.out.println("Short date formats");
System.out.println(usEnglishDate);
System.out.println(usHindiDate);
System.out.println(usJapaneseDate);
System.out.println("Medium date formats");
System.out.println(usEnglishDateLong);
System.out.println(usHindiDateLong);
System.out.println(usJapaneseDateLong);
}
}
Satya - Monday, November 15, 2004 5:28:16 PM
What it produces
Short date formats
11/15/04
11/15/04
04/11/15
Medium date formats
November 15, 2004
November 15, 2004
2004/11/15
Satya - Friday, April 07, 2006 5:02:05 PM
Parsing a date ex:
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date date = formatter.parse("1996.07.10 AD at 15:08:56 PDT");
Satya - Friday, April 07, 2006 5:04:46 PM
Or ...
DateFormat a = DateFormat.getDateInstance(DateFormat.SHORT);
Data date = a.parse();
Satya - Friday, April 07, 2006 5:14:25 PM
working with both time and date
DateFormat a =
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
Date date = a.parse("11/4/03 8:14 PM");
satya - Wednesday, November 07, 2007 12:18:20 PM
how do I get the current locale
Locale curLocale = Locale.getDefault();
String sCurLocale = curLocale.toString();
satya - Wednesday, November 07, 2007 12:24:36 PM
Sample for date formatter
// Format the current time.
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
// Parse the previous string back into a Date.
ParsePosition pos = new ParsePosition(0);
Date currentTime_2 = formatter.parse(dateString, pos);
satya - Thursday, September 17, 2009 4:56:53 PM
Validate date example
//valid dates
//1/1/2009
//11/11/2009
//invalid dates
//13/1/2009
//1/32/2009
public static boolean validateDate(String dateString)
throws ParseException
{
try
{
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
Date date = df.parse(dateString);
return true;
}
catch(ParseException x)
{
return false;
}
}
satya - Thursday, September 17, 2009 4:57:44 PM
understand simple date format
satya - Thursday, September 17, 2009 5:02:58 PM
getting a string from a date
Date d = getDate(sdate);
DateFormat a = getDateFormat();
String s = a.format(d)
satya - Monday, June 14, 2010 3:34:13 PM
what is lenient?
Specify whether or not date/time interpretation is to be lenient. With lenient interpretation, a date such as "February 942, 1996" will be treated as being equivalent to the 941st day after February 1, 1996. With strict interpretation, such dates will cause an exception to be thrown.
satya - Monday, June 14, 2010 3:34:29 PM
java date lenient
java date lenient
satya - Monday, June 14, 2010 4:12:48 PM
Getting the current time
public static Calendar getCurrentTime()
{
Calendar cal = Calendar.getInstance();
return cal;
}
The returned calendar will be initialized with the current moments date and time.
satya - Monday, June 14, 2010 4:13:56 PM
Get me a calendar instance at a given hour today
public static Calendar getTodayAt(int hours)
{
Calendar today = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.clear();
int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
//represents the day of the month
int day = today.get(Calendar.DATE);
cal.set(year,month,day,hours,0,0);
return cal;
}
satya - Monday, June 14, 2010 4:14:46 PM
You can print a calendar/time here
public static String getDateTimeString(Calendar cal)
{
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
df.setLenient(false);
String s = df.format(cal.getTime());
return s;
}
satya - Wed Oct 31 2012 13:55:43 GMT-0400 (Eastern Daylight Time)
You can do this
//To seriailze
Calendar c;
long millis = c.getTimeInMillis();
//serialize millis
//unserialize
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(millis);
satya - 8/14/2020, 10:33:09 AM
What on earth!!! Things I write long ago
What on earth!!! Things I write long ago
satya - 8/14/2020, 10:33:12 AM
2020 now
2020 now
satya - 8/14/2020, 10:33:29 AM
what is setLenient in Java date time processing
what is setLenient in Java date time processing
satya - 8/14/2020, 10:55:59 AM
Strange, comments on leniency before was indeed as they documented it
Strange, comments on leniency before was indeed as they documented it
satya - 8/14/2020, 10:56:37 AM
It appears it means....
Let java decide how to interpret date as it chooses! strange.
satya - 8/14/2020, 3:03:33 PM
Here is java date leniency discussed
satya - 8/14/2020, 3:05:16 PM
DateFormat Java API docs
DateFormat Java API docs
satya - 8/14/2020, 3:19:53 PM
Some similar classes
DateFormat
Calendar
TimeZone
Date
DateFormat
SimpleDateFormat
satya - 8/14/2020, 3:20:48 PM
Looks like Date is deprecated in preference for a Calendar, kind of....
Looks like Date is deprecated in preference for a Calendar, kind of....
satya - 8/14/2020, 3:26:28 PM
Some useful powerstring utils I had before
<#
***************************************************
* datetime utils
***************************************************
#>
function datetime.getNow()
{
[DateTime]$datetimeObj = Get-Date
return $datetimeObj
}
function datetime.getPastDateTime($pastDays)
{
vutils.notnull $pastDays
[DateTime]$now = datetime.getNow
$pastTime = $now.AddDays(-$pastDays)
return $pastTime
}
function datetime.getDateTimeObjForYearMonthDay($year, $month, $day)
{
return [DateTime]::new($year, $month, $day)
}
function datetime.getRandom()
{
$hour = Get-Random -Maximum 23 -Minimum 0
$min = Get-Random -Maximum 59 -Minimum 0
$sec = Get-Random -Maximum 59 -Minimum 0
$date = Get-Date
$d2 = $date.Date
$d2 = $d2.AddHours($hour)
$d2 = $d2.AddMinutes($min)
$d2 = $d2.AddSeconds($sec)
return $d2
}
function datetime.sampleQtr($datetimeObj)
{
[DateTime]$d = $datetimeObj
$date = $d.Date
$hour = $d.Hour
$min = $d.Minute
$qtr = $null
switch ($min) {
{$_ -ge 0 -and $_ -lt 15} {$qtr = 0;break }
{$_ -ge 15 -and $_ -lt 30} {$qtr = 15;break }
{$_ -ge 30 -and $_ -lt 45} {$qtr = 30;break }
{$_ -ge 45 -and $_ -lt 60} {$qtr = 45;break }
}
if ($qtr -eq $null) {throw "Invalid. quarter cannot be null"}
$nd = $date.AddHours($hour)
$nd = $nd.AddMinutes($qtr)
return $nd
}
class classDuration
{
[DateTime]$start
[DateTime]$end
[string]ToString(){
return ("start: {0}, End {1}" -f `
$this.start, `
$this.end)
}
}#end of class
class classDurationDateStrings : classDuration {
classDurationDateStrings(){}
classDurationDateStrings([classDuration]$d)
{
$this.start = $d.start
$this.end = $d.end
}
[String]startDateString(){
return $this.start.ToString("yyyy-MM-dd")
}
[String]endDateString(){
return $this.end.ToString("yyyy-MM-dd")
}
[string]ToString(){
return ("start: {0}, End {1}" -f `
$this.startDateString(), `
$this.endDateString())
}
}
function datetime.convertDurationToString($d)
{
$st = convertToISOTime2 -datetime $d.start
$end = convertToISOTime2 -datetime $d.end
return "start:{0}, end:{1}" -f $st, $end
}
function datetime.getPreviousQtrDuration($datetimeObj)
{
[DateTime]$end = datetime.sampleQtr -datetimeObj $datetimeObj
$begin =$end.AddMinutes(-15)
$duration = [classDuration]::new()
$duration.start = $begin
$duration.end = $end
return $duration
}
function datetime.getFilenameSegment($datetimeObj)
{
#2020-06-25_10-45-03
vutils.notnull $datetimeObj
$s = $datetimeObj.ToString("yyyy-MM-dd_HH-mm-ss")
return $s
}
function datetime.getDateForISOString($isoDateString)
{
return [DateTime]::ParseExact($isoDateString, "yyyy-MM-dd HH:mm:ss",$null)
}
function datetime.getDateForISOStringMilliseconds($isoDateString)
{
return [DateTime]::ParseExact($isoDateString, "yyyy-MM-dd HH:mm:ss.fffffff",$null)
}
function datetime.convertToISOTimeMilliseconds2($datetime)
{
if ($datetime -eq $null)
{
throw "Date time cannot null: from convertToISOTime2"
}
$iso = $datetime.ToString("yyyy-MM-dd HH:mm:ss.fffffff")
return $iso
}
<#
***************************************************
* End: datetime utils
***************************************************
#>