Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_util / DbfUtil.java @ 22037

History | View | Annotate | Download (1.77 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp_util;
2

    
3
import java.text.DateFormat;
4
import java.text.ParseException;
5
import java.text.ParsePosition;
6
import java.text.SimpleDateFormat;
7
import java.util.Date;
8
import java.util.GregorianCalendar;
9

    
10
import org.apache.log4j.Logger;
11

    
12
/**
13
 * Utility static methods.
14
 * 
15
 * @author jldominguez
16
 *
17
 */
18
public class DbfUtil {
19
        
20
        private static Logger logger = Logger.getLogger(DbfUtil.class);
21
        
22
        /**
23
         * Parses a string into a Date object.
24
         * 
25
         * @param str
26
         * @return
27
         */
28
        public static Date stringToDate(String str) {
29
                Date resp = null;
30

    
31
                try {
32
                        resp = (new SimpleDateFormat()).parse(str);
33
                } catch (ParseException e) {
34
                        return alternativeDateParser(str);
35
                }
36
                return resp;
37
        }
38

    
39
        private static Date alternativeDateParser(String str) {
40
                // TODO Auto-generated method stub
41
                if (str.length() == 8) {
42
                        
43
                        try {
44
                                String y = str.substring(2, 4);
45
                                String m = str.substring(4, 6);
46
                                String d = str.substring(6);
47
                                int yy = 1900 + Integer.parseInt(y);
48
                                int mm = ((Integer.parseInt(m)-1) % 12);
49
                                int dd = Integer.parseInt(d);
50
                                
51
                                GregorianCalendar gcal = new GregorianCalendar(yy, mm, dd);
52
                                Date resp = gcal.getTime();
53
                                return resp;
54

    
55
                        } catch (Exception ex) {
56
                                logger.error("Error while parsing: " + ex.getMessage());
57
                                logger.error("Returned date: 1, 1, 1970");
58
                        }
59
                        
60
                } 
61
                
62
                return new Date(0);
63
        }
64

    
65
        private static String getMonthStr(int mm) {
66
                
67
                switch (mm) {
68
                case 0:
69
                        return "Jan";
70
                case 1:
71
                        return "Feb";
72
                case 2:
73
                        return "Mar";
74
                case 3:
75
                        return "Apr";
76
                case 4:
77
                        return "May";
78
                case 5:
79
                        return "Jun";
80
                case 6:
81
                        return "Jul";
82
                case 7:
83
                        return "Aug";
84
                case 8:
85
                        return "Sep";
86
                case 9:
87
                        return "Oct";
88
                case 10:
89
                        return "Nov";
90
                case 11:
91
                        return "Dec";
92
                }
93
                return "Jan";
94
        }
95

    
96

    
97

    
98
}