Dec 02

How to add two dates in java?

We use calendar class add method two add number of days , months and years to Date.

public class AddDatesExample {

public static void main(String[] args) {

Calendar cal = Calendar.getInstance();

cal.set(2012, 11, 23);

System.out.println(“Printing Date before adding ” + cal.getTime());

cal.add(Calendar.DATE, 23);

cal.add(Calendar.MONTH, 12);

cal.add(Calendar.YEAR, 3);

System.out.println(“Printing Date after adding ” + cal.getTime());

}

}

 

Dec 02

How to create Date Object in java?

We use Calendar class to create Date object in java. Calendar.getTime() will return Date Object in java.

 

import java.util.Calendar;

 

public class CreateDateExample {

public static void main(String[] args) {

Calendar calendar = Calendar.getInstance();

calendar.set(2012, 11, 21, 2, 30);

System.out.println(calendar.getTime());

}

 

}

 

Dec 02

How to get month names in java?

We use DateFormatSymbols class to display month names in java.

importjava.text.DateFormatSymbols; 

public class DisplayMonthsExample {

public static void main(String[] args) {

DateFormatSymbols symbols = new DateFormatSymbols();

            String monthNames[] = symbols.getMonths();

for (String monthName : monthNames) {

System.out.println(monthName);

}

}

 

}

 

Output:

January

February

March

April

May

June

July

August

September

October

November

December

 

 

Dec 02

How to round decimal values to nearest number in java?

 

import java.math.RoundingMode;

import java.text.DecimalFormat;

import java.text.NumberFormat;

 

public class DecimalFormatterExample {

public static void main(String[] args) {

double a = 12.234567890;

NumberFormat numberFormatter = DecimalFormat.getInstance();

numberFormatter.setMaximumIntegerDigits(2);

numberFormatter.setMaximumFractionDigits(3);

numberFormatter.setRoundingMode(RoundingMode.DOWN);

System.out.println(“With Rounding mode down”

+ numberFormatter.format(a));

numberFormatter.setRoundingMode(RoundingMode.UP);

System.out.println(“With Rounding mode down”

+ numberFormatter.format(a));

}

 

}

 

 

 

Dec 02

How to convert Date to String Example in java?

 

importjava.text.DateFormat;import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class DateToStringExample {

public static void main(String[] args) throws ParseException {

String str = “12/25/2012”;

DateFormat df = new SimpleDateFormat(“mm/dd/yyyy”);

            Date date = df.parse(str);

String dateString = df.format(date);

System.out.println(“DateString ” + dateString);

}

}

 

Dec 02

How to convert String to Date in java?

We use SimpleDateFormatter calss which takes String format. We have parse() method in DateFormat class used to convert Date.

public class StringToDateExample {

public static void main(String[] args) throws ParseException {

String dateString = “11-July-12”;

Date date;

DateFormat formatter = new SimpleDateFormat(“dd-MMM-yy”);

date = formatter.parse(dateString);

System.out.println(” String converted to Date ” + date);

}

}