Book Image

Hands-On Automation Testing with Java for Beginners

Book Image

Hands-On Automation Testing with Java for Beginners

Overview of this book

Java is one of the most commonly-used software languages by programmers and developers. Are you from a non-technical background and looking to master Java for your automation needs? Then Hands-On Automation Testing with Java for Beginners is for you. This book provides you with efficient techniques to effectively handle Java-related automation projects. You will learn how to handle strings and their functions in Java. As you make your way through the book, you will get to grips with classes and objects, along with their uses. In the concluding chapters, you will learn about the importance of inheritance and exceptions with practical examples. By the end of this book, you will have gained comprehensive knowledge of Java.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

The Calendar class


In the previous section, we explored the Date class, where we learned about Date methods and how to operate on them using simple date format standards. In this section, we will learn about the Calendar class, which is similar to the Date class, but with some extra features. Let's see what they are and how we can use them to extract our date formats using the Calendar class.

First, we will create a class with a different name to avoid conflict. To create a Calendar instance, run the following:

Calendar cal=Calendar.getInstance();
Date d=new Date();

The steps are similar to those for the Date class. However, the Calendar object has some unique features that date doesn't support. Let's explore them.

Use the following code snippet:

        Calendar cal=Calendar.getInstance();
        SimpleDateFormat sd=new SimpleDateFormat("M/d/yyyy hh:mm:ss");
        System.out.println(sd.format(cal.getTime()));

The output for the preceding code will be:

Output displaying date and time using calendar...