Java Fundamental period - Lecture 7

Java Data Manipulation : How to manage data thanks to Java ?

Goal of this lecture

This lecture will help in the realization of your project, we will see how to :

Record data in a file, usual need

A recurrent need in all programs is to perform technical traces, while the program is running
Those traces can be output in several format :
This is featured is usually called Logging

What a Log class should do ?

A Log class (or a Logger) should output messages giving precise information on what is currently happening in the program
Regarding a particular event, the following information should be available

1st part: The Date util

Java holds a lot of Utilities in its JDK

1st part: The Date util (2)

In the logger example we will need those pieces of codes
package fr.tbr.exercises.example;

import java.util.Date;

public class DateExample {

	public static void main (String[] args){
		//Creates a date variable of type Date, the default constructor
		//initializes the date variable with the date of the date
		Date date = new Date();
		System.out.println(date);
	}
}
Outputs something like
Sun May 25 16:28:28 CEST 2014

1st part: The Date util (3)

We will need the SimpleDateFormat Object

1st part: The Date util (4)

The SimpleDateFormat class is an helperclass handling the conversion of Date from/to String
package fr.tbr.exercises.example;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

	public static void main (String[] args){
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss.SSS");
		String stringResult = simpleDateFormat.format(date);
		System.out.println(stringResult);
	}
}
This will output the following (much more accurate) result
2014/05/25 - 16:58:10.302

1st part: The Date util (5)

Notice that you can create a Date object from a String thanks to the SimpleDateFormat
package fr.tbr.exercises.example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

	public static void main (String[] args){
		//Creates a date variable of type Date, the default constructor
		//initializes the date variable with the date of the date
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss.SSS");
		String stringInput = "2014/05/25 - 16:58:10.302";
		try {
			Date date = simpleDateFormat.parse(stringInput);
			//Use the date Object
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}

Aside - The Exception

Notice the try{]catch(Exception ex){} construct : this is the way errors are handled in Java.
We will dedicate a full lecture on that subject, for now consider that it is an error case

1st part: The Date util (6)

Useful Date APIs :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss.SSS");
String stringInput = "2014/05/25 - 16:58:10.302";

Date date1 = new Date();
Date date2 = simpleDateFormat.parse(stringInput);

date2.after(date1); //true if date2 is after date1
date2.before(date1); //true if date2 is before date1
date2.setTime(date1.getTime()); //initializes the date2 with the value of the date1.
        
Beware, although, as the Date class is old, a lot of APIs are deprecated (meaning that they are not recommended to use)

Exercise

Create a Java program that asks a date upon a given format to the user, and checks if the input enforces the pattern

2nd part: The String object

We now need to format properly a String variable. You already got an overview of what could be the usage of the String objects
Creation
String firstString = "Sample String";
String secondString = "Second String";
Concatenation
System.out.println("Testing concatenation");
String concatResult = firstString + " " + secondString;
System.out.println(concatResult);

2nd part: The String object (2)

Replacement
System.out.println("Testing replacement");
concatResult = concatResult.replaceAll("St" , "" );
System.out.println(concatResult);
Immutability : While other objects are "mutable" (their internal values can change during the program execution), Strings are immutable. Format
//Full spec here
//http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
String formattedString = String.format("%tD", new Date());
System.out.println(formattedString); 

2nd part: The String object (3)

You may also need one convenience class: StringBuilder, which allows the developer to "build" a String
StringBuilder sb = new StringBuilder(concatResult);
stringBuilder.append("blah").append(" ").append(new Date()); 
Mutability : What is convenient with the StringBuilder, is that it is mutable. It allows to pass the StringBuilder to methods, and it is modified by the operations done in the method Body

Exercise

Write a class IamLog having one method which signature is
public void log(String message){
 //Do something here
}
The result of the call should be an output in the System.out at this format:
2014/05/25 - 20:57:24.160 : Beginning of the program