Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Tuesday, June 2, 2020

PostgreSQL: Get the TimeStamp of today, yesterday, tomorrow, and a number of days ago or later

To get the current timestamp:

       select current_timestamp;
       or
       select now();

To get the timestamp of the beginning of today:

       select current_date::timestamp;
       or
       select 'today'::timestamp;

To get the timestamps of yesterday and tomorrow:

      select 'yesterday':timestamp, 'tomorrow'::timestamp;
      or
      select (current_date - INTERVAL '1 day')::timestamp, (now0 + INTERVAL '1 day')::timestamp;

To get the timestamps of n days ago:

      select (current_date - INTERVAL 'n day')::timestamp;

To get the timestamp of m days later:

      select (current_date + INTERVAL 'm day')::timestamp;
      select (current_date + INTERVAL '1 month - 3 day')::timestamp
 
Also, see PostgreSQL: Get the TimeStamp of the beginning of a year, a month, or a day

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

Monday, December 4, 2017

Java 8: Three ways to calculate days between two dates

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTest101 {

    public static void main(String[] args){
        String dt2 = "2017335";
        String dt1 = "2016335";
     
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyDDD");
        LocalDate dat2 = LocalDate.parse(dt2, pattern);
        LocalDate dat1 = LocalDate.parse(dt1, pattern);
        System.out.println("dat2: "+dat2.toString()+", dat1: "+dat1);
     
        //Method 1
        long daysBetween1 = dat1.until(dat2, ChronoUnit.DAYS);
        System.out.println("daysBetween1: "+ daysBetween1);
     
        //Method 2
        long daysBetween2 = ChronoUnit.DAYS.between(dat1, dat2);
        System.out.println("daysBetween2: "+ daysBetween2);
     
        //Method 3
        long daysBetween3 = Duration.between(dat1.atStartOfDay(), dat2.atStartOfDay()).toDays();
        System.out.println("daysBetween3: "+ daysBetween3);
    }
}

Reference:

1. Calculate days between two dates in Java 8

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, November 11, 2016

Solved - PostgreSQL: add days or hours to date

PostgreSQL does not have the DateAdd function. It uses the INTERVAL type to add days and hours to a date.

1. Add days to a date.

      select <the date> + cast(<the days> || ' days' as INTERVAL) AS nDate from <your table>;
      Or
      select <the date> + (<the days> * interval '1 day') AS nDate from <your table>;

2. Add hours to a date

      select <the date> + cast(<the hours> || ' hours' as INTERVAL) AS nDate from <your table>;
      Or
      select <the date> + (<the hours> * interval '1 hour') AS nDate from <your table>;

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

   

Monday, December 15, 2014

Increase/Decrease the month by 1 and get the last day of the month in Java

Use the roll method of Calendar to increment the month generated correct dates regardless of the actual days in the moth. For example, use the roll method to increase the month by 1 on a Calendar instance of date January 31, 2012 will give February 28, 2012.

The following sample code shows how to use the roll method to increment or decrement the month and then get the last day of the month.

int year = 2012;
int month = Calendar.JANUARY;
int day = 31;

Calendar calendar = new GregorianCalendar(year, month, day);

//increments the month by 1
calendar.roll(Calendar.MONTH, true);

//or decrements the month by one
//calendar.roll(Calendar.MONTH, false);

//get the last day of the month
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

-------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.