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);

Example of Date formats

Date Format

Calendar

Date


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);
	}
}

Short date formats
11/15/04
11/15/04
04/11/15

Medium date formats
November 15, 2004
November 15, 2004
2004/11/15

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");

DateFormat a = DateFormat.getDateInstance(DateFormat.SHORT);
Data date = a.parse();

more examples


DateFormat a = 
DateFormat.getDateTimeInstance(
            DateFormat.SHORT, DateFormat.SHORT);
Date date = a.parse("11/4/03 8:14 PM");

Locale curLocale = Locale.getDefault();
String sCurLocale = curLocale.toString();

// 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);

//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;
		}
	}

understand simple date format


Date d = getDate(sdate);
DateFormat a = getDateFormat();
String s = a.format(d)

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.

java date lenient

Search for: java date lenient


public static Calendar getCurrentTime()
{
  Calendar cal = Calendar.getInstance();
  return cal;
}

The returned calendar will be initialized with the current moments date and time.


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;
}

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;
}

//To seriailze
Calendar c;
long millis = c.getTimeInMillis();
//serialize millis


//unserialize
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(millis);

What on earth!!! Things I write long ago

2020 now

what is setLenient in Java date time processing

Search for: what is setLenient in Java date time processing

Strange, comments on leniency before was indeed as they documented it

Let java decide how to interpret date as it chooses! strange.

Here is java date leniency discussed

DateFormat Java API docs

Search for: DateFormat Java API docs

Java Date Format API

Calendar API

SimpleDateFormat API


DateFormat
Calendar
TimeZone
Date
DateFormat
SimpleDateFormat

Looks like Date is deprecated in preference for a Calendar, kind of....

System.getCurrentTimeMillis


<#
***************************************************
* 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
***************************************************
#>