Java program to find Week of Year

How to find Week of Year:

The following Java program is to find week of the year from Calendar object. Using getInstance() method the current date is obtained from Calendar object and using the below code the week from the calendar is obtained.

Syntax: int week=cal.get(Calendar.WEEK_OF_YEAR);

week.java


package com.candidjava.time;

import java.util.Calendar;
import java.util.Date;

public class Week 
{
public static void main(String[] args) 
{
	Calendar cal=Calendar.getInstance();
	int week=cal.get(Calendar.WEEK_OF_YEAR);
	System.out.println("week of year:" +week);	
}
}

Output:

week of year:2

Java Program to Convert String into Date Object and Date into String

Java program to convert String into Date object and Date into String:

The following Java program is used to convert Date from String into Date object and Date object to String.

String to Date Object,

Syntax: Strings=”28/01/2019″;

SimpleDateFormat sdf=new SimpleDateFormat(“dd/MM/yyyy”);

Date d=sdf.parse(s);

Get the date in String format and convert it into Date Object using parse() method. SimpleDateFormat is used to get Date in the particular format.

Date object to String,

Syntax: Date d1=new Date();

String s1=sdf1.format(d1);

The current date is obtained using date object and is converted to String using format() method.

stringformat.java

package com.candidjava.time;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StringFormat 
{
	public static void main(String[] args) throws ParseException 
	{
		//String to date 
		String s="28/01/2019";
		SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
		Date d=sdf.parse(s);
		System.out.println(d);
		
		//date to string 
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd");
		Date d1=new Date();
		String s1=sdf1.format(d1);
		System.out.println(s1);
	}

}

Output:

Mon Jan 28 00:00:00 IST 2019
2019/01/07

Java program to convert Long to Date Object and Date Object to Long

How to convert Long to Date Object and Date Object to Long:

The following Java program is used to convert date from long to Date object and Date object to long.

Long to Date Object,

Syntax: Long l=1346524199000l;

Date d=new Date(l);

Get the Date in long datatype and convert to Date object using the above code.

Date object to long,

Syntax: Long l1=d1.getTime();

Create a Date object and get current date using Date(). Convert the obtained date to long using getTime() method.

longformat.java

package com.candidjava.time;

import java.text.ParseException;
import java.util.Date;

public class LongFormat 
{
public static void main(String[] args) throws ParseException
{
	//long to date object
	Long l=1346524199000l;
	Date d=new Date(l);
	System.out.println(d);
	
	//date to long
	Date d1=new Date();
	Long l1=d1.getTime();
	System.out.println(l1);
}
}

Output:

Sat Sep 01 23:59:59 IST 2012
1546848563069

Java program to calculate Age from Date of birth

Java program to calculate Age from Date of birth The following Java program is to calculate age from date of birth. The age can be calculated using different ways.

Method 1: Direct age calculation

Using the package java.time.LocalDate the age is calculated when the dob is known. using LocalDate.now() the current date is obtained. using the method between() in java.time.Period the current date and dob are compared. The final age can be obtained using diff.getYears(), diff.getMonths() and diff.getDays() methods.

Syntax: Period diff=Period.between(l, now);

Method 2: Using Calendar object

Using Calendar object we can also calculate age when dob is in String format. Convert the String to Date object and use getInstance() and setTime() methods to assign date to calendar objects. using the below code the year, month and date are obtained separately.

Syntax:

int year=c.get(Calendar.YEAR);

int month=c.get(Calendar.MONTH)+1;

int date=c.get(Calendar.DATE);

Using between() method in java.time.Period package the year, month and date obtained from calendar object is compared to current date obtained by LocalDate.now() method.We can get the age using diff.getYears() method.

Dobconversion.java

package com.candidjava.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

public class DobConversion 
{
public static void main(String[] args) throws ParseException 
{
	//direct age calculation 
	LocalDate l=LocalDate.of(1998, 04, 23); //specify year, month, date directly
	LocalDate now=LocalDate.now(); 		//gets localDate
	Period diff=Period.between(l, now);	//difference between the dates is calculated
	System.out.println(diff.getYears() +"years" +diff.getMonths() +"months" +diff.getDays() +"days");

	//using Calendar Object
	String s="1994/06/23";
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
	Date d=sdf.parse(s);
	Calendar c=Calendar.getInstance();
	c.setTime(d);
	 int year=c.get(Calendar.YEAR);
	 int month=c.get(Calendar.MONTH)+1;
	 int date=c.get(Calendar.DATE);
	LocalDate l1 = LocalDate.of(year, month, date);
	LocalDate now1 = LocalDate.now();
	Period diff1 = Period.between(l1, now1);
	System.out.println("age:" +diff1.getYears() +"years");
}
}

Output:

20years8months15days
age:24years

Java program to convert java.util.Date to java.sql.Date and vice versa

Here learn how to convert java.util.Date to java.sql.Date. The following Java program is to convert current date from java.util.Date to java.sql.Date and vice versa.

java.util.Date to java.sql.Date

Syntax: java.util.Date ud=new java.util.Date();

Using the above code util Date is obtained.

Synatx: java.sql.Date sd=new java.sql.Date(ud.getTime());

Using getTime() method utilDate is converted to sqlDate.

java.sql.Date to java.util.Date

Syntax: java.sql.Date sd1=new java.sql.Date(System.currentTimeMillis());

The above code shows the sqlDate which is obtained using System.currentTimeMillis() method.

Using the same getTime() method it is converted to util Date.


package com.candidjava.time;

public class DateUtilToSql 
{
public static void main(String[] args) 
{
	//java.util.Date to java.sql.Date
	java.util.Date ud=new java.util.Date();
	java.sql.Date sd=new java.sql.Date(ud.getTime());
	System.out.println(sd);
	
	//java.sql.Date to java.util.Date
	java.sql.Date sd1=new java.sql.Date(System.currentTimeMillis());
	java.util.Date ud1=new java.util.Date(sd1.getTime());
	System.out.println(ud1);	
}
}

Output:

2019-01-07
Mon Jan 07 13:32:01 IST 2019

Java program to convert Calendar to Date and Date to Calendar

How to convert Calendar to Date and Date to Calendar:

In this tutorial shows how to converting calendar to date, The following Java program is to convert from Calendar object to Date object and Date object to Calendar object.

Calendar object to Date object,

Using Calendar.getInstance() method the date from calendar object is obtained and using getTime() method it is converted to Date object.

Date object to Calendar object,

Date d=new Date(1515660075000l);

The above code is used to get date and time from date object. using getInstance() and setTime() method it is converted to Calendar object. The date is obtained using cal.getTime() method.

CalendarToDate.java

package com.candidjava.time;

import java.util.Calendar;
import java.util.Date;

public class CalendarToDate
{
	public static void main(String[] args) 
	{
		//Calendar Object to Date Object
		 Calendar c=Calendar.getInstance();		
		 Date d1=c.getTime();
		 	System.out.println(d1);
		 	
		//Date Object to Calendar Object
		Date d=new Date(15156600750001);
		Calendar cal=Calendar.getInstance();
		cal.setTime(d);
		System.out.println(cal.getTime());
	}
}

Output:

Mon Jan 07 13:18:32 IST 2019
Mon Jan 07 13:18:32 IST 2019

Java program to get current Date and Time

This tutorials about Java program to get current date and time. There are different ways to get Current Date and Time in Java.

Methods for getting current date and time:

Method 1: Using the package java.util.Date

Syntax: java.util.Date date=new java.util.Date();

Method 2: Using the package java.sql.Date

Syntax: java.sql.Date date1=new java.sql.Date(System.currentTimeMillis());

Method 3: Using the package java.time.LocalDate to get local date

Syntax: java.time.LocalDate.now()

Method 4: Using the package java.time.LocalTime to get local time

Syntax: java.time.LocalTime.now()

Method 5: Using package java.time.LocalDateTime to get both date and time

Syntax: java.time.LocalDateTime.now()

Method 6: Using the package java.time.clock

Syntax: java.time.Clock.systemUTC().instant()

Method 7: Using the package java.time.Calendar

Syntax: Calendar.getInstance().getTime()

Method 8: The package java.text is used to get date and time in simple date format from calendar object.

Syntax: SimpleDateFormat sdf=new SimpleDateFormat(“dd/MM/yyyy hh:mm:ss”);

DateAndTime.java


package com.candidjava.time;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateAndTime 
{
public static void main(String[] args)
{
	//using package java.util to get date and time 
	java.util.Date date=new java.util.Date();
	System.out.println("Date:" +date);
	
	//using package java.sql to get date and time
	java.sql.Date date1=new java.sql.Date(System.currentTimeMillis());
	System.out.println("Date1:" +date1);

	//using java.time.LocalDate
	System.out.println(java.time.LocalDate.now());
	
	//using java.time.LocalTime
	System.out.println(java.time.LocalTime.now());
	
	//using java.time.LocalDateTime
	System.out.println(java.time.LocalDateTime.now());
	
	//using java.time.clock 
	System.out.println(java.time.Clock.systemUTC().instant());
	
	//using package java.util.calendar
	System.out.println(Calendar.getInstance().getTime());
	
	//using package java.text
	SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
	String c=sdf..format(Calendar.getInstance().getTime());
	System.out.println(c);
}
}

Output:

Date:Mon Jan 07 13:30:47 IST 2019
Date1:2019-01-07
2019-01-07
13:30:47.401
2019-01-07T13:30:47.401
2019-01-07T08:00:47.401Z
Mon Jan 07 13:30:47 IST 2019
07/01/2019 01:30:47

Java program to add days to current date

Java program to add days to the current date. The following Java program is to add one day to the current date. Adding days to the present date can be done using a different method.

Method 1: Using LocalDate object: LocalDate ld=LocalDate.now(); The above code is used to obtain the current date. Use plusDays(1) method to add one day to the current date.

Method 2: Using Calendar object: Create a calendar object and use getInstance() method to get the current date. Add one day to CalendarDate using (Calendar.Date, 1) and get the new date using getTime() method.

Method 3: Using Date object Using Date object the next date is obtained using the below code, Date tmw=new Date(tdy.getTime()+(1000*60*60*24));

Java add days:


package com.candidjava.time;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class AddDate 
{
public static void main(String[] args) 
{
	//add one day to LocalDate Object
	LocalDate ld=LocalDate.now();	//get current date
	ld=ld.plusDays(1);	//add one day to current date
	System.out.println(ld);
		
	// add one day to calendar object
	Calendar c=Calendar.getInstance();
	c.add(Calendar.DATE, 1);	//add one day to calendar object
	System.out.println(c.getTime()); //print added date
	
	
	// add one day to date object
	Date tdy=new Date();
	Date tmw=new Date(tdy.getTime()+(1000*60*60*24)); //get tomorrow date
	System.out.println(tmw);
}
}

Output:

2019-01-08
Tue Jan 08 13:01:02 IST 2019
Tue Jan 08 13:01:02 IST 2019

 

Java Date Time Calendar Tutorials

Following Simple programming examples for the various actions using date time and calendar functions.

Java program to add days to current date

Java program to get current Date and Time

Java program to convert Calendar to Date and Date to Calendar

Java program to convert java.util.Date to java.sql.Date and vice versa

Java program to calculate Age from Date of birth

Java program to convert Long to Date Object and Date Object to Long

Java Program to Convert String into Date Object and Date into String

Java program to find Week of Year

JSP Interview Questions and Answers

Here is most frequently asked jsp interview questions with answers. Following jsp interview questions are deeply covered for both freshers and experience on interview basis.

1.What is JSP and why do we need it?

Java Server page is like HTML page connecting to the servlet in back-end and contains some java code inside the scriptlet tag. JSP pages are mainly used to create dynamic web pages easily. It is a lightweight code.

2. Difference between HTML and JSP Comment?

  • HTML comment tag is <!– Html comment –> and JSP comment tag is <%– jsp comment –%>
  • HTML comment are called output comment whereas JSP comment are called hide comment.
  • When we view source file, HTML comments are displayed whereas JSP comments are not displayed.

3. Difference between JSP and servlet?

  • Java server page is HTML based code and servlet is java code.
  • Servlet is faster than JSP.
  • Servlet accept all request but JSP accepts only HTTP request.

4. Explain JSP Lifecycle?

The JSP pages follow the following phases:

1) Request from the JSP (index.jsp) file is loaded.

2) Translation phase

The index.jsp page is converted into index_jsp.java file.

3) Compilation phase

The index_jsp.java is converted into index_jsp.class file.

4) The index_jsp.class is loaded and the output response is displayed on the browser page.

5. Explain the types of directives in JSP?

There are three types of directives in JSP. They are,

  • Page directive

It defines the page dependent attribute like scripting language,

error page, etc and it’s applied to the whole jsp page.

Syntax:

<%@ page attribute= value”%>

  • Include directive

It adds file resources into the JSP page. File attribute is used to include resource during translation time.

Syntax:

<%@ include file=resourceName” %>

  • Taglib directive

The JSP taglib directive is used to define a tag library which defines many tags and are used in the JSP pages by JSP standard tag libraries.

Syntax:

<%@ taglib uri=uriofthetaglibrary”

prefix=prefixofthelibrary” %>

6. What is Scriptlet, Expression, and Declaration in JSP?

The scriptlet, expression, and declaration are used as JSP tags.

Scriptlet tag

They are used to place java code inside the JSP pages.

Syntax: <% java code %>

Expression tag

It is mainly used to get the values of variables or methods.

Syntax: <%=expression %>

Declaration tag

It is used to define methods in jsp pages.

Syntax: <%! Declaration %>

7. Is it possible to use java comment in JSP?

Yes, java comment (single line comment, multiline comment) can be used in JSP pages but only inside the scriptlet tag.

8. What are JSP implicit objects?

JSP implicit object is the Java Objects and they are predefined variables used directly in JSP pages. There are 9 JSP implicit objects. They are out, request, response, session, page, application, config, page context, exception.

9. Use of page implicit object?

It is an implicit object of the type Object class. Page implicit object is used to indicate the current JSP page.

10. Use of application implicit object?

It is an implicit object of type Servlet Context. It is used to get initialization the parameter to use in all JSP pages.

11. Which implicit object is not available in normal JSP pages?

Exception implicit object is not available in normal JSP pages. It is used in error pages to catch the exception thrown in the page.

12. What are the benefits of the Page Context implicit object?

  • It is only implicit object common in both JSP implicit objects and JSP EL objects.
  • It is used to set or get the attribute from other scopes.

13. What is JSP Expression Language and what is it benefits?

JSP Expression Language is used to get parameter values from java bean class. For using this JSP EL it should be enabled in page directives in JSP page. Its benefit is code reduction.

Syntax: ${expression}

14. What are JSP EL implicit objects and how it’s different from JSP implicit Objects?

The JSP EL implicit objects are pageScope, sessionScope, applicationScope, cookie, pageContext, requestScope, param, paramValues, initParam, header, headerValue.

15. How do we print “<br> creates a new line in HTML” in JSP?

<

out.print(“<br> creates a new line in HTML”) 

%>

16. How to ignore the EL expression evaluation in a JSP?

To ignore EL expression in a JSP page, the page directive attribute having isELIgnored should be set to true value.

17. How can we avoid direct access to JSP pages from client browser?   

We can avoid direct access of JSP pages from client browser by placing the JSP pages inside the WEB-INF. It can be accessed only by using a request dispatcher servlet.

18. What is the difference between include directive and JSP: include action?       

       Include Directive

It adds file resources into the JSP page.

File attribute is used to include resource during translation time.

jsp: include action

It adds page response into JSP page.

Page attribute is used to add resources during request time.

19. Can we use JSP implicit objects in JSP Declaration?                

No, we can’t use JSP implicit objects inside JSP declaration.