Last active
August 29, 2015 14:16
-
-
Save ViCMAP/9f039d425ce03faf4f38 to your computer and use it in GitHub Desktop.
Dumping an object in Java. I'm just simulating the var_dump method from PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vicabreu.test; | |
import java.lang.reflect.Field; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
public class MainClass { | |
private String firstName; | |
private String lastName; | |
private int age; | |
private String birthDay; | |
public static void main(String[] args) throws ParseException, IllegalArgumentException, IllegalAccessException{ | |
String firstName = "Victor"; | |
String lastName = "Abreu"; | |
String birthDay = "1983-04-11"; | |
MainClass obj = new MainClass(firstName, lastName, birthDay); | |
// getting object dumped | |
Field[] fields = obj.getClass().getDeclaredFields(); | |
System.out.println("\n" + obj.getClass().getSimpleName()); | |
for (int i=0; i<fields.length; i++) | |
{ | |
System.out.println("\t[" + fields[i].getName() + "] (" + fields[i].getType() + ") = " + fields[i].get(obj)); | |
} | |
/*System.out.println("You name is " + obj.getFirstName() + " " + obj.getLastName()); | |
System.out.println("Your birthday is on " + obj.getbirthDay()); | |
System.out.println("Your age is " + obj.getAge());*/ | |
} | |
public MainClass(String firstName, String lastName, String birthDay) throws ParseException{ | |
this.setFirstName(firstName); | |
this.setLastName(lastName); | |
this.setbirthDay(birthDay); | |
this.calculateAge(); | |
} | |
private void calculateAge() throws ParseException{ | |
Calendar todayDate = GregorianCalendar.getInstance(); | |
Calendar birthDay = GregorianCalendar.getInstance(); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | |
todayDate.setTime(new Date()); | |
birthDay.setTime(formatter.parse(this.getbirthDay())); | |
birthDay.add(Calendar.DAY_OF_YEAR, - todayDate.get(Calendar.DAY_OF_YEAR)); | |
int age = todayDate.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR); | |
this.setAge(age); | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public int getAge() { | |
return age; | |
} | |
private void setAge(int age) { | |
this.age = age; | |
} | |
public String getbirthDay() { | |
return birthDay; | |
} | |
public void setbirthDay(String birthDay) { | |
this.birthDay = birthDay; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment