Posts

Showing posts with the label Date

Java - How to convert String to Date

Image
In this section, we will show you h ow to  convert String to java.util.Date  in Java. Refer to table below for some of the common date and time patterns used in java.text. SimpleDateFormat , refer to this  JavaDoc 1. String = 11-July-2023 Main.java import java.text.ParseException ; import java.text.SimpleDateFormat ; import java.util.Date ; public class Main { public static void main ( String [] argv) { SimpleDateFormat formatter = new SimpleDateFormat( "dd-MMM-yyyy" ); String dateInString = "11-July-2023" ; try { Date date = formatter .parse( dateInString ); System . out .println( date ); System . out .println( formatter .format( date )); } catch ( ParseException e) { e.printStackTrace(); } } } Console Output: Tue Jul 11 00:00:00 IST 2023 11-Jul-2023   2. String = 11/09/2023 Main.java import java.text.ParseException ; import java.text.SimpleDateFormat ;

How to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to date in Go Language. The time package in Go language provides Add() function which  is used to add/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, The signature of the Add method is: func (t Time) Add(d Duration) Time Example 1: Go Program to add/subtract minutes. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 30 minutes dt2 := dt1.Add( 30 * time.Minute) fmt.Println( "After 30 minutes:" , dt2) //Subtract 3 minutes dt3 := dt1.Add(- 3 * time.Minute) fmt.Println( "Before 3 minutes:" , dt3) } Output: Example 2: Go Program to add/subtract seconds. package main import (

How to add/subtract days, months or years to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract days, months or years to a date in Go Language. The time package in Go language provides AddDate() function which is used to add/subtract years, months and days to time t.  The signature of the AddDate method is: func (t Time) AddDate(years int , months int , days int ) Time Example 1: Go Program to add/subtract days. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 3 days dt2 := dt1.AddDate( 0 , 0 , 3 ) fmt.Println( "After three days: " , dt2) //Subtract 400 days dt3 := dt1.AddDate( 0 , 0 , - 400 ) fmt.Println( "Before 400 days: " , dt3) } Output: Example 2: Go Program to add/subtract months. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now()

Go Language - Get Current Date and Time

Image
Hello everyone, today we are going to learn how to get the current date and time in Go Language. We need to import the “time” package in our Go script to work with date and time. Go Program to find current Date and Time: package main import ( "fmt" "time" ) func main() { dt := time.Now() fmt.Println(dt.String()) //Formatted Date and Time //Format MM/DD/YYYY fmt.Println(dt.Format( "01/02/2006" )) //Format MM-DD-YYYY fmt.Println(dt.Format( "01-02-2006" )) //Format MM.DD.YYYY fmt.Println(dt.Format( "01.02.2006" )) //Format MM-DD-YYYY hh:mm:ss fmt.Println(dt.Format( "01-02-2006 15:04:05" )) //With ShortWeek Day fmt.Println(dt.Format( "01-02-2006 Mon" )) //With weekday (Monday) fmt.Println(dt.Format( "01-02-2006 15:04:05 Monday" )) //Include micro seconds fmt.Println(dt.Format( "01-02-2006 15:04:05.000000" )) //Include n