Time & Date Representation in Java. Date class An object of type Date represents an instance in time Part of java.util.* (requires import statement) A.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
CS 180 Recitation 9 / {06, 07} / Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
Type Safety and Enumerations. Type Checking and Type Errors  The type system defines data types and the operations on data types.  A type error occurs.
Java Classes Using Java Classes Introduction to UML.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 14 Internationalization F Processing Date and Time –Locale –Date –TimeZone –Calendar and GregorianCalendar –DateFormat and SimpleDateFormat F Formatting.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 26 Internationalization.
Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat.
CIS 338: Operators and Formatting in VB.NET Dr. Ralph D. Westfall April, 2011.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
JAC444: Dates Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Programming
Chapter 14 Internationalization F Processing Date and Time –Locale –Date –TimeZone –Calendar and GregorianCalendar –DateFormat and SimpleDateFormat F Formatting.
Conversion Functions.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
CiS 260: App Dev I. 2 Introduction to Methods n A method (or ________) is a segment of code that performs a specific task. n Advantages of modular program.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
1 Chapter 20 Internationalization. 2 Objectives F To describe Java's internationalization features (§ 20.1). F To construct a locale with language, country,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import.
Java Util Package Prepared by, S.Amudha AP/SWE. Calendar 1.The abstract Calendar class provides a set of methods that allows you to convert a time in.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
25 Jan 2011 CMPT166 Dr. Sean Ho Trinity Western University
Basic Utility Classes U Abd. Rohim, MT mailto:
Chapter 14 Internationalization
Objects and Classes.
Java String and Date ISYS 350.
Interactive Standard Input/output
Chapter 4 Intermediate (part 1)
CS170 ygao JAVA, C7.
Servlet Date Operations
Time Revision.
Java Date ISYS 350.
Prepared by, S.Amudha AP/SWE
Java Classes and Objects 3rd Lecture
Date Functions Farrokh Alemi, Ph.D.
Date-time.
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Java Date ISYS 350.
2008 Calendar.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Java Date ISYS 350.
Getting Started with Java
Classes and Objects Object Creation
Java Date ISYS 350.
2008 Calendar.
Presentation transcript:

Time & Date Representation in Java

Date class An object of type Date represents an instance in time Part of java.util.* (requires import statement) A new Date object is automatically set to the time of its creation, to the millisecond For example: Date now = new Date(); // object now holds value // of the current millisecond

Converting to String Like many other Java standard classes, Date includes a toString() method, which allows us to output the value of a Date object in the form: Day Mon dd hh:mm:ss TMZ yyyy –Day is a three-letter abbreviation for day of week – e.g. Sat, Sun, etc. –Mon is a three-letter abbreviation for month of year – e.g. Jan, Feb, etc. –dd is the one- or two-digit date –hh:mm:ss is the current time –TMZ is the time zone – for example,, a Date object created at Kirkwood would have a CDT or CST stamp –yyyy is the 4-digit year

SimpleDateFormat class The java.text.* package contains a class that allows us to output Date objects using other formats – the SimpleDateFormat class interacts with Date objects much like the DecimalFormat class interacts with floating-point numbers A Date object is created, then a SimpleDateFormat object is created to describe its output appearance

Example import java.util.*; import java.text.*; public class DemoDate { public static void main (String [] args) { Date now; SimpleDateFormat fmt; now = new Date(); fmt = new SimpleDateFormat ("dd MMM yyyy"); System.out.println ("Today's date: " + fmt.format(now); }

SimpleDateFormat patterns – a partial list PatternResult "yyyy.MM.dd G 'at' HH:mm:ss z" AD at 12:08:56 CST "hh 'o''clock' a, zzzz" 12 o'clock PM, Central Standard Time "yyyyy.MMMMM.dd hh:mm aaa"02006.February.01 12:08 PM "EEE, MMM d, ''yy" Wed, Feb 4, '06 More examples:

GregorianCalendar class The architects of Java changed their minds about how the Date class should work during the evolution of the language Several methods that were previously part of the Date class have since moved to other classes that describe this kind of data, notably the GregorianCalendar class

GregorianCalendar class Also from java.util.* More friendly than Date because it uses named constants Incorporates a Date object, accessible via the getTime() method Like a Date object, a GregorianCalendar object will have the current time as its value by default; it is relatively easy to create an object using a different moment in time

Example GregorianCalendar myDay = new GregorianCalendar(1978, 4, 16); This creates an object that contains the Date value May 16, 1978 (my 18 th birthday) Note that the month of May is represented by a 4 rather than a 5; this is because the first month, January, is represented by a 0

Example We can use a predefined constant from the Calendar class to represent the month, as follows: GregorianCalendar myDay = new GregorianCalendar (1978, Calendar.MAY, 16); Calendar.MAY is another example of a class constant, like Math.PI

Extracting Date information from a GregorianCalendar object Using other predefined constants from the Calendar class and the GregorianCalendar object’s get() method, we can extract various pieces of information about the underlying Date value The Calendar class constants include Calendar.DAY_OF_WEEK, Calendar.DATE, Calendar.MONTH, Calendar.DAY_OF_YEAR and Calendar.YEAR

Example GregorianCalendar myDay = new GregorianCalendar(1978, 4, 16); System.out.println((myDay.get(Calendar.MONTH)+1) + “/” + myDay.get(Calendar.DATE) + “/” + myDay.get(Calendar.YEAR)); // prints 5/16/1978