Date Snippets

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Date Snippets as PDF for free.

More details

  • Words: 392
  • Pages: 2
-Creating a Date object for a specific calendar date. import java.util.Calendar; import java.util.Date; Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25); Date date = xmas.getTime(); ============================================================================= - Current date as a Date object import java.util.Date; Date today = new Date((new Date()).getTime()); ============================================================================= - Format the date to a string // Use the current date as the transaction date. java.util.Date today = new java.util.Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); sdf.setLenient(false); strDate = sdf.format(today); ============================================================================= - Parse a custom format string into a Date object. try { DateFormat formatter = new SimpleDateFormat("MM/dd/yy"); Date date = (Date)formatter.parse("01/29/02"); formatter = new SimpleDateFormat("dd-MMM-yy"); date = (Date)formatter.parse("29-Jan-02"); // Parse a date and time; see also // e317 Parsing the Time Using a Custom Format formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); date = (Date)formatter.parse("2002.01.29.08.36.33"); formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); date = (Date)formatter.parse("Tue, 29 Jan 2002 22:14:02 -0500"); } catch (ParseException e) { } ============================================================================= - Format a Date object. Format formatter; // The year formatter = new SimpleDateFormat("yy"); formatter = new SimpleDateFormat("yyyy");

// 02 // 2002

// The month formatter = new SimpleDateFormat("M");

// 1

formatter = new SimpleDateFormat("MM"); formatter = new SimpleDateFormat("MMM"); formatter = new SimpleDateFormat("MMMM");

// 01 // Jan // January

// The day formatter = new SimpleDateFormat("d"); formatter = new SimpleDateFormat("dd");

// 9 // 09

// The day in week formatter = new SimpleDateFormat("E"); formatter = new SimpleDateFormat("EEEE");

// Wed // Wednesday

// Get today's date Date date = new Date(); // Some examples formatter = new SimpleDateFormat("MM/dd/yy"); String s = formatter.format(date); // 01/09/02 formatter = new SimpleDateFormat("dd-MMM-yy"); s = formatter.format(date); // 29-Jan-02 // Examples with date and time; see also // e316 Formatting the Time Using a Custom Format formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); s = formatter.format(date); // 2002.01.29.08.36.33 formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); s = formatter.format(date); // Tue, 09 Jan 2002 22:14:02 -0500 ============================================================================= - Number of seconds between 2 dates. -- Get Julian date number from first date (DATE_1) NDATE_1 := TO_NUMBER(TO_CHAR(DATE_1, 'J')); -- Get Julian date number from second date (DATE_2) NDATE_2 := TO_NUMBER(TO_CHAR(DATE_2, 'J')); -- Get seconds since midnight from first date (DATE_1) NSECOND_1 := TO_NUMBER(TO_CHAR(DATE_1, 'SSSSS')); -- Get seconds since midnight from second date (DATE_2) NSECOND_2 := TO_NUMBER(TO_CHAR(DATE_2, 'SSSSS')); RETURN (((NDATE_2 - NDATE_1) * 86400)+(NSECOND_2 - NSECOND_1)); // inline version seconds_diff := (((TO_NUMBER(TO_CHAR(DATE_2, - TO_NUMBER(TO_CHAR(DATE_1, + (TO_NUMBER(TO_CHAR(DATE_2, - TO_NUMBER(TO_CHAR(DATE_1,

'J')) 'J'))) * 86400) 'SSSSS')) 'SSSSS'))));

Related Documents

Date Snippets
June 2020 8
Date
June 2020 29
Ruby Code Snippets
August 2019 25
Date
June 2020 33
: : Date :
May 2020 48