Date & Time Handling In JAVA

Similar documents
Packages Inner Classes

Declaring Array Variable

Class Library java.util Package. Bok, Jong Soon

6.Introducing Classes 9. Exceptions

Lesson:9 Working with Array and String

Data Types, Literals, Operators

// Create a configuration object and set the name of the application SparkConf conf=new SparkConf().setAppName("Spark Exam 2 - Exercise

Instructor s Notes Java - Beginning Built-In Classes. Java - Beginning Built-In Classes

Java - Dates and Threads

Getting Started With Java

EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Introduction to Objects. James Brucker

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Lecture Notes: ESC 101

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Android Debug Framework

Internationalization and Localization

CS 180. Recitation 8 / {30, 31} / 2007

COMP Assignment #4 (Due: Mon. Mar 12 noon)

Chapter 13 Abstract Classes and Interfaces

Attempting to display a frame window

Types of Views. View category Purpose Examples of views. Display a particular type of content, such as an image or text.

Flexible Rate Card Importer

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Abstract Classes Interfaces

Motivations. Chapter 13 Abstract Classes and Interfaces

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Builder pattern. A creational pattern

Chapter 8 Objects and Classes Part 1

Formatted Output Pearson Education, Inc. All rights reserved.

Flexible CSV CDR Importer

CSC-140 Assignment 6

Date and Time Functions

9Introducing. Concurrency with Threads

Use appropriate existing abstract data types to provide a clear and correct program solution.

ENGLISH Page 1 of 6. EXAM IN COURSE TDT4100 Object-Oriented Programming / IT1104 Programming, Advanced Course. Tuesday 29. Mai

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Groovy For Java Programmers

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

From the book. Integration-Ready Architecture and Design. Cambridge University Press

Topic 03 - Objects and Classes. I. Classes and Objects Casual Preview. Topic 03. VIII. Benefits of Encapsulation. XIII. Overloading methods, Signature

Terms: MediaPlayer, VideoView, MediaController,

Chapter 13 Abstract Classes and Interfaces

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

OO Programming Concepts

CS313D: ADVANCED PROGRAMMING LANGUAGE

FUNDAMENTALS OF PROGRAMMING LANGUAGES II. Shree Ramchandra Education Society s SHREE RAMCHANDRA COLLEGE OF ENGINEERING, LONIKAND, PUNE

Lab Assignment 13 (week 13)

1.00 Lecture 14. Lecture Preview

AIMMS Function Reference - Date Time Related Identifiers

//

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

CS313D: ADVANCED PROGRAMMING LANGUAGE

1.00 Tutorial 3. Methods, Classes Arrays & ArrayLists. September 26 & 27, 2005

CS 116. Lab Assignment # 1 1

public static boolean isoutside(int min, int max, int value)

An account object will contain an array of transactions and will be associated with a customer. The class diagram is shown below:

Ohad Barzilay and Oranit Dror

Chapter 9 Abstract Classes and Interfaces

Definition: A thread is a single sequential flow of control within a program.

JAVA. Design patterns

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

IT 313 Advanced Application Development Midterm Exam

App SandBox Directory

Commerce Analytics Service On-boarding and Technical Guide

JAVA - FILE CLASS. The File object represents the actual file/directory on the disk. Below given is the list of constructors to create a File object

Flex Development. Flex Time

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Class 14: Introduction to the Swing Toolkit

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Text Properties Data Validation Styles/Themes Material Design

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #4. Review: Methods / Code

ISO8601. Time.ISO8601. Time.ISO8601

Dates and Times in Java. Tecniche di Programmazione A.A. 2014/2015

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Formatting cells. Microsoft Excel Cell alignment. Format cells options

Chapter 10 How to work with dates

Creating an Immutable Class. Based on slides by Prof. Burton Ma

CSC 1214: Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Final Exam Practice Questions

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Object- Oriented Analysis, Design and Programming


CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Advanced Object Concepts

Introduction to Programming Using Java (98-388)

Working with Temporal Data in ArcGIS

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Preview from Notesale.co.uk Page 9 of 108

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Ch.8 Sockets for Clients

Clock Commands on the Cisco IOS XR Software

Lecture 6. Drinking. Nested if. Nested if s reprise. The boolean data type. More complex selection statements: switch. Examples.

Clock Commands on the Cisco IOS XR Software

Transcription:

Date & Time Handling In JAVA Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 1

Date and Time API System.currentTimeMillis() java.util.date java.util.calendar java.text.simpledateformat A static method that returns the current date and time as milliseconds since January 1st 1970 A class that represents a date and time A base class for calendar classes. Has methods to do date and time arithmethics like adding a day or month to another date A class that can help you parse String's into Date's, and format Date's as String's www.sisoft.in 2

java.util.date Java provides the Date class available in java.util package, this class encapsulates the current date and time. The Date class supports two constructors: 1) Date( ) This constructor initializes the object with the current date and time. 2) Date(long millisec) This constructor accepts an argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970 www.sisoft.in 3

Getting Current Date and Time This is a very easy method to get current date and time in Java. You can use a simple Date object with tostring() method to print the current date and time as follows import java.util.date; public class DateDemo { public static void main(string args[]) { // Instantiate a Date object Date date = new Date(); // display time and date using tostring() System.out.println(date.toString()); } } Output : on May 04 09:51:52 CDT 2009 www.sisoft.in 4

Date Formatting Using SimpleDateFormat SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. import java.util.*; import java.text.*; public class DateDemo { public static void main(string args[]) { Date dnow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.mm.dd 'at' hh:mm:ss a zzz"); System.out.println("Current Date: " + ft.format(dnow)); } } Output : Current Date: Sun 2004.07.18 at 04:14:09 PM PDT www.sisoft.in 5

Simple DateFormat Format Codes To specify the time format, use a time pattern string. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following Character Description Example M Month in year July or 07 d Day in month 10 h Hour in A.M./P.M. (1~12) 12 H Hour in day (0~23) 22 m Minute in hour 30 s Second in minute 55 S Millisecond 234 E Day in week Tuesday D Day in year 360 www.sisoft.in 6

Measuring Elapsed Time Sometimes, we may need to measure point in time in milliseconds. See below Example: import java.util.*; public class DiffDemo { public static void main(string args[]) { try { long start = System.currentTimeMillis( ); System.out.println(new Date( ) + "\n"); catch (Exception e) { System.out.println("Got an exception!"); } } } Output: Thread.sleep(5*60*10); System.out.println(new Date( ) + "\n"); long end = System.currentTimeMillis( ); long diff = end - start; System.out.println("Difference is : " + diff); } www.sisoft.in Sun May 03 18:16:51 GMT 2009 Sun May 03 18:16:57 GMT 2009 Difference is : 5993 7

Date & Time Another Examples www.sisoft.in 8

1. Example: Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); String date = sdf.format(new Date()); System.out.println(date); //15/10/2013 2. Example: Convert String to Date. SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String dateinstring = "31-08-1982 10:20:56"; Date date = sdf.parse(dateinstring); System.out.println(date); //Tue Aug 31 10:20:56 SGT 1982 9

3. Example: Get current date time: SimpleDateFormat d= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println(d.format(date)); //2013/10/15 16:16:39 10

Calender Class www.sisoft.in 11

Introduction The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.following are the important points about Calendar: This class also provides additional fields and methods for implementing a concrete calendar system outside the package. Calendar defines the range of values returned by certain calendar fields. 12

Class constructors : 1) protected Calendar() This constructor constructs a Calendar with the default time zone and locale. 2) protected Calendar(TimeZone zone, Locale alocale) This constructor constructs a calendar with the specified time zone and locale. 13

Calender Examples www.sisoft.in 14

Example: 1 Date to Calendar (settime()) DateFormat formatter = new SimpleDateFormat("yyyyMMdd"); date = (Date)formatter.parse(date.toString()); cal=calendar.getinstance(); cal.settime(date); 15

Example: 2 Calendar to Date ( gettime()) Calendar cal = Calendar.getInstance(); cal.add(calendar.date, 1); Date date = cal.gettime(); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); String date1 = format1.format(date); 16

Example: 2 Set a date manually SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss"); Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56); System.out.println(sdf.format(calendar.getTime())); //update a date calendar.set(calendar.year, 2014); calendar.set(calendar.month, 11); calendar.set(calendar.minute, 33); System.out.println("#2. " + sdf.format(calendar.gettime())); Output : 2013 Feb 28 13:24:56 #2. 2014 Dec 28 13:33:56 17