Java中的Date、Calendar、Logger、Annotation

来源:互联网 发布:手机自动挂机赚钱软件 编辑:程序博客网 时间:2024/06/08 02:18

import java.io.*;import java.text.*;import java.util.*;public class DateCanlendar {public static void main(String[] args) {// TODO Auto-generated method stubPrintStream out=System.out;Date date=new Date();DateFormat dateformat1=new SimpleDateFormat("EE-MM-dd-yyyy");//简短信息格式DateFormat dateformat2=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);//长信息格式DateFormat dateformat3=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);//详细信息格式DateFormat dateformat4=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);//增加区域显示Locale locale=new Locale("en","US");DateFormat dateformat5=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,locale);//使用Calendar,得到具体的时间Calendar now1=Calendar.getInstance();Calendar now2=Calendar.getInstance(locale);out.println(System.currentTimeMillis());out.println(date.getTime());out.println(date.toString());out.println(dateformat1.format(date));out.println(dateformat2.format(date));out.println(dateformat3.format(date));out.println(dateformat4.format(date));out.println(dateformat5.format(date));out.println(now1.get(Calendar.YEAR));out.println(now1.get(Calendar.WEEK_OF_MONTH));out.println(now2.get(Calendar.YEAR));}}

结果:

1351509222407
1351509222206
Mon Oct 29 19:13:42 CST 2012
星期一-10-29-2012
12-10-29 下午7:13
2012年10月29日 下午07时13分42秒
2012年10月29日 星期一 下午07时13分42秒 CST
Monday, October 29, 2012 7:13:42 PM CST
2012
5
2012


import java.text.MessageFormat;import java.util.*;import java.util.logging.ConsoleHandler;import java.util.logging.Handler;import java.util.logging.Level;import java.util.logging.LogRecord;import java.util.logging.Logger;import java.io.*;public class Practice{public static void main(String[] args){PrintStream out=System.out;Logger log=Logger.getLogger("Practice");try{out.println(args[0]);}catch(ArrayIndexOutOfBoundsException a){log.warning("Logging and Logger");}///////////////////////////////////////////////////////设置等级,默认等级为INFO,否则在INFO下的不显示ConsoleHandler handler=new ConsoleHandler();handler.setLevel(Level.ALL);    log.setLevel(Level.ALL);    log.addHandler(handler);//Logging的等级log.severe("严重信息");log.warning("警示信息");log.info("一般信息");log.config("设定方面信息");log.fine("细微的信息");log.finer("更细微的信息");log.finest("最细微的信息");        ///////////////////////////////////////////////////////另一种指定等级显示的方法log()log.log(Level.SEVERE,"严重信息log()方法");log.log(Level.WARNING,"警示信息log()方法");log.log(Level.INFO,"一般信息log()方法");log.log(Level.CONFIG,"设定方面信息log()方法");log.log(Level.FINE,"细微的信息log()方法");log.log(Level.FINER,"更细微的信息log()方法");log.log(Level.FINEST,"最细微的信息log()方法");//5个默认的Handle:ConsoleHandler、FileHandler、StreamHandler、SocketHandler、MemoryHandlerout.println(log.getName());out.println(log.getParent());for(Handler h:log.getHandlers())    out.println(h+"XXX");for(Handler h:log.getParent().getHandlers())out.println(h+"YYY");//格式化信息String message="Hello,{0}!This is your first {1}";Object[] params=new Object[]{"Kute","Java"};MessageFormat format=new MessageFormat(message);out.println(format.format(params));//国际化信息Locale/*信息的绑定ResoutceBundle,在Classpath路径选新建一个message.properties文件,用于存储信息,文件中输入以下内容: welcome=Hello name=world *///上述保存的是键值对,之后可在程序中使用键来获取值,以后至只要在文件修改内容即可,而不必在程序中修改ResourceBundle resource=ResourceBundle.getBundle("D:\\message.properties");out.println(resource.getString("welcome")+","+resource.getString("name"));}}//自定义Format,用来自定义日志的输出格式class Formattest extends Formatter{public String format(LogRecord logRecord){return "LogRecord info";}}

结果:

十月 30, 2012 5:41:00 下午 Practice main
警告: Logging and Logger
十月 30, 2012 5:41:00 下午 Practice main
严重: 严重信息
十月 30, 2012 5:41:00 下午 Practice main
严重: 严重信息
十月 30, 2012 5:41:00 下午 Practice main
警告: 警示信息
十月 30, 2012 5:41:00 下午 Practice main
警告: 警示信息
十月 30, 2012 5:41:00 下午 Practice main
信息: 一般信息
十月 30, 2012 5:41:00 下午 Practice main
信息: 一般信息
十月 30, 2012 5:41:00 下午 Practice main
配置: 设定方面信息
十月 30, 2012 5:41:00 下午 Practice main
详细: 细微的信息
十月 30, 2012 5:41:00 下午 Practice main
较详细: 更细微的信息
十月 30, 2012 5:41:00 下午 Practice main
非常详细: 最细微的信息
十月 30, 2012 5:41:00 下午 Practice main
严重: 严重信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
严重: 严重信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
警告: 警示信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
警告: 警示信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
信息: 一般信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
信息: 一般信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
配置: 设定方面信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
详细: 细微的信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
较详细: 更细微的信息log()方法
十月 30, 2012 5:41:00 下午 Practice main
非常详细: 最细微的信息log()方法
Practice
java.util.logging.LogManager$RootLogger@ff9053
java.util.logging.ConsoleHandler@5c7734XXX
java.util.logging.ConsoleHandler@96212aYYY
Hello,Kute!This is your first Java

Hello,world


import java.util.*;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.*;import java.io.*;import java.lang.*;public class Practice{PrintStream out=System.out;//重写父类方法@Override//不建议使用@Deprecated//抑制编译器警告@SuppressWarnings(value = { "unchecked" })public String toString(){return "Annotation测试";}//自定义Annotationpublic @interface Debug1{}public @interface Debug2{String value();}public @interface Debug3{String[] value();}//子类是否继承父类的annotation@Retention(RetentionPolicy.RUNTIME)//这是必须的一步@Inheritedpublic @interface Debug4{String value() default "这是默认初始值";}public @interface Debug5{public enum Current{NONE,REQUIRE,ANALSIS,DESIGN,SYSTEM};Current current() default Current.SYSTEM;String str();boolean ok();}@Retention(RetentionPolicy.RUNTIME)//加入到API文件@Documented//限定起适用的时机,比如class、interface、field、method、constructor等@Target({ElementType.CONSTRUCTOR,ElementType.METHOD,ElementType.class})public @interface Debug6{String value();String name();}@Debug1@Debug2("dddddd")@Debug3({"bbbb","aaaa"})@Debug4()@Debug5(current=Debug5.Current.NONE,str="hahahaha",ok=false)@Debug6(name = "ababab", value = "cecece")public void use(){out.println("这里用于测试自定义Annotation");}public static void main(String[] args){System.out.println("这是一个测试Annotation的程序");}}










原创粉丝点击