4.Date
发表日期:2021-06-30 19:37:09 | 来源: | | 浏览(728) 分类:JAVA基础
DEMO1
import java.util.Date; public class DateDemo01 extends Date { /** * */ private static final long serialVersionUID = 1L; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); System.out.println(date);//Tue Sep 10 15:43:13 CST 2013 } }
DEMO2
import java.util.Calendar; import java.util.GregorianCalendar; public class DateDemo02 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Calendar calendar = new GregorianCalendar(); System.out.println("year:"+calendar.get(Calendar.YEAR));//year:2013 System.out.println("month:"+(calendar.get(Calendar.MONTH)+1));//month:9 System.out.println("day:"+calendar.get(Calendar.DAY_OF_MONTH));//day:10 System.out.println("hour:"+calendar.get(Calendar.HOUR_OF_DAY));//hour:15 System.out.println("minute:"+calendar.get(Calendar.MINUTE));//minute:50 System.out.println("second:"+calendar.get(Calendar.SECOND));//second:30 System.out.println("millksecond:"+calendar.get(Calendar.MILLISECOND));//Millisecond:862 } }
DEMO3
import java.text.DateFormat; import java.text.FieldPosition; import java.text.ParsePosition; import java.util.Date; public abstract class DateDemo03 extends DateFormat { public static void main(String[] args) { // TODO Auto-generated method stub DateFormat dateFormat1 = DateFormat.getDateInstance(); DateFormat dateFormat2 = DateFormat.getDateTimeInstance(); System.out.println("Date:"+dateFormat1.format(new Date()));//Date:2013-9-10 System.out.println("DateTime:"+dateFormat2.format(new Date()));//DateTime:2013-9-10 16:01:54 } }
DEMO4
import java.text.DateFormat; import java.text.FieldPosition; import java.text.ParsePosition; import java.util.Date; import java.util.Locale; public class DateDemo04 extends DateFormat { /** * */ private static final long serialVersionUID = 1L; @Override public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition fieldPosition) { // TODO Auto-generated method stub return null; } @Override public Date parse(String source, ParsePosition pos) { // TODO Auto-generated method stub return null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DateFormat dateFormat1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN")); DateFormat dateFormat2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN")); System.out.println("Date:"+dateFormat1.format(new Date()));//Date:2013年9月10日 System.out.println("DateTime:"+dateFormat2.format(new Date()));//DateTime:2013年9月10日 下午04时05分59秒 CST } }
DEMO5
//丢了
DEMO6
import java.text.SimpleDateFormat; import java.util.Date; import com.sun.org.apache.bcel.internal.generic.NEW; public class DateDemo06 extends SimpleDateFormat { /** * */ private static final long serialVersionUID = 1L; /** * @param args */ public static void main(String[] args) { //new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()) // TODO Auto-generated method stub String strDate = "2013-9-10 16:07:28.345"; String par1 = "yyyy-MM-dd HH:mm:ss.SSS"; String par2 = "yyyy年MM月dd日 HH使mm分ss秒SSS毫秒"; SimpleDateFormat sFormat1 = new SimpleDateFormat(par1); SimpleDateFormat sFormat2 = new SimpleDateFormat(par2); Date date = null; try { date = sFormat1.parse(strDate); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } System.out.println(date); System.out.println(sFormat2); System.out.println(sFormat2.format(date)); System.out.println(sFormat2.format(new Date())); System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH使mm分ss秒SSS毫秒").format(new Date())); } }
DEMO7
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo07 { public static void main(String[] args) throws ParseException { String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()); //System.out.println(date); String string = "2015年11月02日 01:37:39"; String par_res = "yyyy年MM月dd日 HH:mm:ss"; SimpleDateFormat resFormat = new SimpleDateFormat(par_res); Date resDateTime = resFormat.parse(string); String par_data = "yyyy-MM-dd"; SimpleDateFormat dataFormat = new SimpleDateFormat(par_data); String par_time = "HH:mm"; SimpleDateFormat timeFormat = new SimpleDateFormat(par_time); System.out.println(dataFormat.format(resDateTime)); System.out.println(timeFormat.format(resDateTime)); } }
- JAVA(0)
- JAVA基础(30)
- 1.JAVA开发环境配置(0)
- 2.java数据类型(0)
- 3.数组(0)
- 4.Date(0)
- 5.String 和 StringBuffer类常用方法(0)
- 6.Math类(0)
- 7.Cloneable(0)
- 8.File 文件(0)
- 9.FileReader和FileWriter(0)
- 10.RandomAccessFile(0)
- 11.FileInputStream和FileOutputStream(0)
- 12.InputStreamReader和OutputStreamWriter(0)
- 13.BufferedReader(0)
- 14.Scanner(0)
- 15.DataOutputStream(0)
- 16.Thead 多线程(0)
- 17.TimerTask(0)
- 18.zip(0)
- 19.Charset(0)
- 20.List(0)
- 21.Map(0)
- 22.Properties(0)
- 23.Enumeration(0)
- 24.Collection(0)
- 25.JDBC(0)
- 26.Iterator(0)
- 27.Abstract(0)
- 28.Interface(0)
- 29.Serializable(0)
- 30.Camparable(0)
- JSP基础(50)