Java Data Manipulation : How to manage data thanks to Java ?
Logging
Log class
should do ?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);
}
}
Sun May 25 16:28:28 CEST 2014
Date
object has a built-in toString()
methodSimpleDateFormat
Object
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);
}
}
2014/05/25 - 16:58:10.302
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();
}
}
}
try{]catch(Exception ex){}
construct : this is the way errors are handled in Java.
try{}
block symbolizes a part of code critical enough
to
throw an exception
.
catch(Exception e){}
block allows to react while concretely
facing that exception
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.
String firstString = "Sample String";
String secondString = "Second String";
ConcatenationSystem.out.println("Testing concatenation");
String concatResult = firstString + " " + secondString;
System.out.println(concatResult);
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);
StringBuilder
, which allows the developer to "build" a StringStringBuilder 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
IamLog
having one method which signature is public void log(String message){
//Do something here
}
2014/05/25 - 20:57:24.160 : Beginning of the program