Saturday, 10 September 2022

Convert java.util.Date to java.sql.Date in Java

java.util.Date

‘java.util.Date’ class is used to represent a specific instant in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time).

 

How to get an instance of Date class?

Following constructors are used to get an instance of java.util.Date class.

 

public Date()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

 

public Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

 

java.util.Date object is not immutable

Date object is mutable, you can change the date using setTime method.

date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

 

Find the below working application.

 

JavaUtilDateDemo.java

 

package com.sample.app.time;

import java.util.Date;

public class JavaUtilDateDemo {

	public static void main(String[] args) {
		Date date1 = new Date();
		Date date2 = new Date(1642516388900l);

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);

		// Update date1 and date2 objects.
		date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
		date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

		System.out.println("\n\ndate1 : " + date1);
		System.out.println("date2 : " + date2);

	}

}

 

Output

date1 : Sat Sep 10 12:20:12 IST 2022
date2 : Tue Jan 18 20:03:08 IST 2022


date1 : Thu Jan 01 05:30:00 IST 1970
date2 : Fri Mar 18 07:28:31 IST 2005

 

java.sql.Date

java.sql.Date class extends java.util.Date class.

 

package java.sql;

public class Date extends java.util.Date {
	...........
	...........
}

 

java.sql.Date class is used to model a Date object whenever we want to work with DATE type in SQL. To conform with the definition of SQL DATE type, the millisecond values wrapped by a java.sql.Date instance are normalized by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

Find the below working application.

 

JavaSqlDateDemo.java

package com.sample.app.time;

public class JavaSqlDateDemo {

	public static void main(String[] args) {
		java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
		java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

		System.out.println("sqlDate : " + sqlDate);
		System.out.println("utilDate : " + utilDate);
	}

}

Output

sqlDate : 2022-01-18
utilDate : Tue Jan 18 20:03:08 IST 2022

Just like java.util.Date class, java.sql.Date class is also mutable.

 

In summary, java.util.Date object stores a date plus time information (Tue Jan 18 20:03:08 IST 2022) as milliseconds since the epoch, whereas java.sql.Date stores date only (2022-01-18).

 

How to convert java.util.Date to java.sql.Date?

Step 1: Get the time in milliseconds.

java.util.Date utilDate = new java.util.Date();
long timeInMillis = utilDate.getTime();

Step 2: Get an instance of java.sql.Date using the milliseconds that we got in step 1.

java.sql.Date sqlDate = new java.sql.Date(timeInMillis);

Find the below working application.

 

DateConverter.java

package com.sample.app.time;

public class DateConverter {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		long timeInMillis = utilDate.getTime();
		
		java.sql.Date sqlDate = new java.sql.Date(timeInMillis);

		System.out.println("utilDate : " + utilDate);
		System.out.println("sqlDate : " + sqlDate);

	}

}

Output

utilDate : Sat Sep 10 13:12:38 IST 2022
sqlDate : 2022-09-10


 

Previous                                                 Next                                                 Home

java.util.Date vs java.sql.Date

java.util.Date

‘java.util.Date’ class is used to represent a specific instant in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time).

 

How to get an instance of Date class?

Following constructors are used to get an instance of java.util.Date class.

 

public Date()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

 

public Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

 

java.util.Date object is not immutable

Date object is mutable, you can change the date using setTime method.

date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

 

Find the below working application.

 


JavaUtilDateDemo.java
package com.sample.app.time;

import java.util.Date;

public class JavaUtilDateDemo {

	public static void main(String[] args) {
		Date date1 = new Date();
		Date date2 = new Date(1642516388900l);

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);

		// Update date1 and date2 objects.
		date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
		date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

		System.out.println("\n\ndate1 : " + date1);
		System.out.println("date2 : " + date2);

	}

}

Output

date1 : Sat Sep 10 12:20:12 IST 2022
date2 : Tue Jan 18 20:03:08 IST 2022


date1 : Thu Jan 01 05:30:00 IST 1970
date2 : Fri Mar 18 07:28:31 IST 2005

java.sql.Date

java.sql.Date class extends java.util.Date class.

package java.sql;

public class Date extends java.util.Date {
	...........
	...........
}

java.sql.Date class is used to model a Date object whenever we want to work with DATE type in SQL. To conform with the definition of SQL DATE type, the millisecond values wrapped by a java.sql.Date instance are normalized by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

Find the below working application.

 

JavaSqlDateDemo.java

package com.sample.app.time;

public class JavaSqlDateDemo {

	public static void main(String[] args) {
		java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
		java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

		System.out.println("sqlDate : " + sqlDate);
		System.out.println("utilDate : " + utilDate);
	}

}

Output

sqlDate : 2022-01-18
utilDate : Tue Jan 18 20:03:08 IST 2022

Just like java.util.Date class, java.sql.Date class is also mutable.

 

In summary, java.util.Date object stores a date plus time information (Tue Jan 18 20:03:08 IST 2022) as milliseconds since the epoch, whereas java.sql.Date stores date only (2022-01-18).



You may like

Interview Questions

How to get a random element from Set?

Extract the elements of specific type in a collection

Method overloading ambiguity with null values in Java

Quick guide to load balancers

Constant folding in Java

Quick guide to assertions in Java

Friday, 9 September 2022

Atlas Client: Get Detailed Glossary including terms and categories

AtlasClientV2#getGlossaryExtInfo method return the complete gloassry information including terms and categories.

 

Signature

public AtlasGlossary.AtlasGlossaryExtInfo getGlossaryExtInfo(String glossaryGuid) throws AtlasServiceException

Example

AtlasGlossary.AtlasGlossaryExtInfo atlasGlossary = atlasClient.getGlossaryExtInfo("844a3e1a-110c-4bef-91bf-622dc1eab3e7");

Find the below working application.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

DetailedGlossary.java

package com.sample.app.glossary;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.AtlasGlossary;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class DetailedGlossary {
  public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
    AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
        new String[] { "admin", "admin" });

    AtlasGlossary.AtlasGlossaryExtInfo atlasGlossary = atlasClient.getGlossaryExtInfo("844a3e1a-110c-4bef-91bf-622dc1eab3e7");
    
    if(atlasGlossary == null) {
      System.out.println("Glossary not exists");
    }else {
      System.out.println("atlasGlossary : \n" + JsonUtil.prettyPrintJson(atlasGlossary));
    }
    
  }
}

Output

atlasGlossary : 
{
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3",
  "shortDescription" : "Demo Glossary",
  "longDescription" : "Demo GLossary.......",
  "terms" : [ {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b"
  } ],
  "categories" : [ {
    "categoryGuid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
    "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3"
  } ],
  "termInfo" : {
    "8d0451be-bfe9-497c-aae9-f1892b156c94" : {
      "guid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
      "qualifiedName" : "term2@myGlosary3",
      "name" : "term2",
      "shortDescription" : "term2...",
      "longDescription" : "term2......",
      "anchor" : {
        "glossaryGuid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
        "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b"
      },
      "glossaryTermHeader" : {
        "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
        "qualifiedName" : "term2@myGlosary3"
      }
    },
    "cd2dbbc5-0440-477e-a6e6-175eb13e3d46" : {
      "guid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
      "qualifiedName" : "term1@myGlosary3",
      "name" : "term1",
      "shortDescription" : "term1...",
      "longDescription" : "term1...........",
      "anchor" : {
        "glossaryGuid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
        "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80"
      },
      "glossaryTermHeader" : {
        "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
        "qualifiedName" : "term1@myGlosary3"
      }
    }
  },
  "categoryInfo" : {
    "80e199fb-f924-4470-adc1-8227b5650f6c" : {
      "guid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
      "qualifiedName" : "category1@myGlosary3",
      "name" : "category1",
      "shortDescription" : "category1...",
      "longDescription" : "category1....",
      "anchor" : {
        "glossaryGuid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
        "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3"
      }
    }
  }
}

  

Previous                                                    Next                                                    Home

Hive: exp: Return e to the power n

Signature

exp(n)

hive> SELECT * FROM emp;
OK
emp.id	emp.name	emp.hobbies	emp.technology_experience	emp.gender_age	emp.rating
1	Hari	["Football","Cricket"]	{"Java":"3.4Yrs","C":"4.5Yrs"}	{"gender":"Male","age":30}	-1.5
2	Chamu	["Trekking","Watching movies"]	{"Selenium":"5.6Yrs"}	{"gender":"Feale","age":38}	3.0
3	Sailu	["Chess","Listening to music"]	{"EmbeddedC":"9Yrs"}	{"gender":"Femle","age":32}	2.5
4	Gopi	["Cricket"]	{"Datastage":"11Yrs"}	{"gender":"Male","age":32}	-0.7
Time taken: 0.041 seconds, Fetched: 4 row(s)
hive> ;
hive> ;
hive> SELECT rating,exp(rating) FROM emp;
OK
rating	_c1
-1.5	0.22313016014842982
3.0	20.085536923187668
2.5	12.182493960703473
-0.7	0.4965853037914095
Time taken: 0.06 seconds, Fetched: 4 row(s)

 


Previous                                                    Next                                                    Home