好记忆力不如烂笔头71-获得当前的方法名,类名,路径等当前信息
好记性不如烂笔头71-获得当前的方法名,类名,路径等当前信息
有时候要用到类对象资深的一些信息,比如方法名称,比如类名,类的路径等,简单整理下。
获得当前JAVA类的方法名称、类名、时间等源代码
package com.tools;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 显示JAVA类的方法名称、类名、时间等
*
* @author 范芳铭
*/
public class MySelf {
public static void main(String[] args){
MySelf self = new MySelf();
//获得当前的方法的名称
String name = Thread.currentThread().getStackTrace()[1].getMethodName();
System.out.println("当前运行的方法名称:" + name);
//获得当前的类的名称
System.out.println("当前的类名:" + self.getClass().getName());
//类的绝对路径
System.out.println("类的绝对路径:" + Class.class.getClass().getResource("/").getPath());
//工程的路径
System.out.println("工程的路径:" + System.getProperty("user.dir"));
//系统的当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("系统的当前时间:" + df.format(new Date()));// new Date()为获取当前系统时间
}
}