使用javaDate类代数据仓库维度表
使用javaDate类代数据仓库维度表
Date类别:
,返回一个相对日期的毫秒数。精确到毫秒。但不支持日期的国际化和分时区显示。
Date 类从Java 开发包(JDK)1.0 就開始进化,当时它仅仅包括了几个取得或者设置一个日期数据的各个部分的方法。 比方说月, 日, 和年。 这些方法如今遭到了批评而且已经被转移到了Calendar类里去了,这样的改进旨在更好的处理日期数据的国际化格式。
Calender类:
相对于Date更加强大的时间类,是抽象类,提供了常规的日期改动功能和国际化支持。
GregorianCalendar类:
提供处理日期的方法。用于计算日期。是Calendar类的子类,实用的是有个推断闰年的方法。
DateFormat类:
能够接受字符串输入、输出。提供了将日期/时间信息进行格式化处理的功能。
SimpleDateFormat类:
功能更强大的日期时间格式化类,自己定义格式化日期和时间。
java.util.Locale类:
描写叙述特定的地理、政治、文化上的地区。Locale对象主要封装了“地区”和“语言种类”双方面 .
 
以下用代码来模拟生成日期维度表的一些字段值:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
 
public class DateUtil {
         Localeloc = new Locale("zh", "CN");
//      Localeloc = new Locale("en", "CN");
         Calendarcalendar = new GregorianCalendar(loc);
        
         publicvoid init(){
                   calendar.set(2014,9, 10);
         }
        
         publicDate add(int day){
                   calendar.add(Calendar.DAY_OF_MONTH,day);
                   returncalendar.getTime();
         }
        
         publicvoid showDate(Date date){
                   p("TimeInMillis",calendar.getTimeInMillis()+"");
                  
                   p("ShortDate",java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT,loc)
                                     .format(date));
                   p("MEDIUMDate",java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM,loc)
                                     .format(date));
                   p("LONGDate",java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG, loc)
                                     .format(date));
                   p("LONGFULL",java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL, loc)
                                     .format(date));
                  
                   SimpleDateFormatsdf = new SimpleDateFormat("D",loc);
                   p("dayin year",sdf.format(date));
                   sdf.applyPattern("d");
                   p("dayin month",sdf.format(date));
                   sdf.applyPattern("EEEE");
                   p("dayname",sdf.format(date));
                   sdf.applyPattern("E");
                   p("dayabbreviation",sdf.format(date));
                  
                   sdf.applyPattern("ww");
                   p("weekin year",sdf.format(date));
                   sdf.applyPattern("W");
                   p("weekin month",sdf.format(date));
                  
                   sdf.applyPattern("MM");
                  String month = sdf.format(date);
                   p("monthnumber",sdf.format(date));
                   sdf.applyPattern("MMMM");
                   p("monthname",sdf.format(date));
                   sdf.applyPattern("MMM");
                   p("monthabbreviation",sdf.format(date));
                  
                   sdf.applyPattern("yy");
                   p("year2",sdf.format(date));  
                   sdf.applyPattern("yyyy");
                   p("year4",sdf.format(date));
                  
                   intquarter_number = (int)Math.ceil( (Integer.parseInt(month) /3.0));
                   p("quertername","Q"+quarter_number);
                  
                   intfirst_day_of_week = calendar.getFirstDayOfWeek();
                   p("is_first_day_of_week",firstWeekDay(first_day_of_week));
         }
        
         publicvoid p(String desc,String date){
                   System.out.println(desc+ " : " + date);
         }
        
         publicvoid calAndShow(){
                   for(int i = 0; i < 3; i++) {
                            init();
                            showDate(add(i));
                            System.out.println();
                   }
         }
        
         publicString firstWeekDay(int first){
                   returnfirst == calendar.get(Calendar.DAY_OF_WEEK) ? "yes" : "no";
         }
        
         publicstatic void main(String[] args) {
                   DateUtild = new DateUtil();
                   d.calAndShow();
                   System.out.println(Math.ceil(11.0/3.0));
         }
} 
    
        版权声明:本文博客原创文章。博客,未经同意,不得转载。
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号