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

How to convert data into calender - java Example

How to convert data into calender - java Example

The following expample expalin's, how to convert data and time in to  calender object


package com.onlinecodegeek.javase.date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateInToCalendar {
public static void main(String[] argv) throws ParseException {

//1. Create a Date from String
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "26-08-2015 10:20:56";
Date date = sdf.parse(dateInString);
DateAndCalendar obj = new DateAndCalendar();

//2. Test - Convert Date to Calendar
Calendar calendar = obj.dateToCalendar(date);
System.out.println(calendar.getTime());

//3. Test - Convert Calendar to Date
Date newDate = obj.calendarToDate(calendar);
System.out.println(newDate);

}

//Convert Date to Calendar
private Calendar dateToCalendar(Date date) {

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;

}

//Convert Calendar to Date
private Date calendarToDate(Calendar calendar) {
return calendar.getTime();
}

}

Wed Aug 26 10:20:56 IST 2015
Wed Aug 26 10:20:56 IST 2015

 

 

Display Date and Time using java

Display Date and Time using java

In this example, we are going look in to details about Date and
Calendar objects. The following example is describes how to display current date and time.

File Name : CurrentDateTime.java

Output :

Example 1

Current Date & Time : 08/03/2014 13:46:19


Example 2

Current Date & Time : 08/03/2014 13:46:19


Dive in to Java Calendar

Dive in to Java Calendar

Date and time is most commonly used concepts in projects. In this post, we look in to the basic concepts of Calendar object and we will discussion about how to use in projects.


The Calendar
is an abstract class that provides methods for converting between a specific instant in time.
  • calendar fields are YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on.

  • It is used for manipulating date and time.

Syntax

Calendar calendar = Calendar.getInstance();



is a concrete class is used for formatting and parsing dates in a locale-sensitive manner.

It allows for formatting (date -> text), parsing (text -> date), and normalization.It also supports localized date and time pattern strings


Date and Time Pattern Result

"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700


Example