99import  java .text .ParseException ;
1010import  java .text .SimpleDateFormat ;
1111
12+ import  java .util .TimeZone ;
13+ 
14+ import  java .lang .Math ;
15+ 
1216/** 
1317 * 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀 
1418 * 优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题 
@@ -29,6 +33,7 @@ public class PartitionByRangeDateHash extends AbstractPartitionAlgorithm impleme
2933    private  long  partionTime ;
3034
3135    private  static  final  long  oneDay  = 86400000 ;
36+     private  static  final  TimeZone  UTC  = TimeZone .getTimeZone ("UTC" );
3237
3338    private  String  groupPartionSize ;
3439    private  int  intGroupPartionSize ;
@@ -40,13 +45,16 @@ public void init()
4045    {
4146        try 
4247        {
43-             beginDate  = new  SimpleDateFormat (dateFormat ).parse (sBeginDate )
44-                     .getTime ();
48+             SimpleDateFormat  sdfInit  = new  SimpleDateFormat (dateFormat );
49+             sdfInit .setTimeZone (UTC );
50+             beginDate  = sdfInit .parse (sBeginDate ).getTime ();
4551            intGroupPartionSize  = Integer .parseInt (groupPartionSize );
4652            formatter  = new  ThreadLocal <SimpleDateFormat >() {
4753                @ Override 
4854                protected  SimpleDateFormat  initialValue () {
49-                     return  new  SimpleDateFormat (dateFormat );
55+                     SimpleDateFormat  sdf  = new  SimpleDateFormat (dateFormat );
56+                     sdf .setTimeZone (UTC ); 
57+                     return  sdf ;
5058                }
5159            };
5260            if  (intGroupPartionSize  <= 0 )
@@ -80,8 +88,7 @@ public Integer calculateStart(String columnValue)
8088    {
8189        try 
8290        {
83-             long  targetTime  = new  SimpleDateFormat (dateFormat ).parse (
84-                     columnValue ).getTime ();
91+             long  targetTime  = formatter .get ().parse (columnValue ).getTime ();
8592            int  targetPartition  = (int ) ((targetTime  - beginDate ) / partionTime );
8693            return  targetPartition  * intGroupPartionSize ;
8794
@@ -96,8 +103,7 @@ public Integer calculateEnd(String columnValue)
96103    {
97104        try 
98105        {
99-             long  targetTime  = new  SimpleDateFormat (dateFormat ).parse (
100-                     columnValue ).getTime ();
106+             long  targetTime  = formatter .get ().parse (columnValue ).getTime ();
101107            int  targetPartition  = (int ) ((targetTime  - beginDate ) / partionTime );
102108            return  (targetPartition +1 ) * intGroupPartionSize   - 1 ;
103109
0 commit comments