码迷,mamicode.com
首页 > 其他好文 > 详细

合理、科学地设计指令格式

时间:2018-07-29 22:34:57      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:容量   cat   style   字符型   ret   main   res   The   seda   

计算机的指令格式与机器的字长、存储器的容量及指令的功能都有很大的关系。从便于程序设计、增加基本操作并行性、提高指令功能的角度来看,指令中应包含多种信息。

但在有些指令中,由于部分信息可能无用,这将浪费指令所占的存储空间,并增加了访存次数,也许反而会影响速度。

因此,如何合理、科学地设计指令格式,使指令既能给出足够的信息,又使其长度尽可能地与机器的字长相匹配,以节省存储空间,缩短取指时间,提高机器的性能,这是指令格式设计中的一个重要问题。

 

  1 package Com.TableText;
  2 
  3     import java.text.DateFormat;  
  4     import java.text.SimpleDateFormat;  
  5     import java.util.Calendar;  
  6     import java.util.Date;  
  7       
  8     /** 
  9      *  
 10      * 功能描述: 
 11      *  
 12      */  
 13     public class TableText_03 {  
 14       
 15         public static Date date = null;  
 16       
 17         public static DateFormat dateFormat = null;  
 18       
 19         public static Calendar calendar = null;  
 20       
 21         /** 
 22          * 功能描述:格式化日期 
 23          *  
 24          * @param dateStr 
 25          *            String 字符型日期 
 26          * @param format 
 27          *            String 格式 
 28          * @return Date 日期 
 29          */  
 30         public static Date parseDate(String dateStr, String format) {  
 31             try {  
 32                 dateFormat = new SimpleDateFormat(format);  
 33                 String dt = dateStr.replaceAll("-", "/");  
 34                 if ((!dt.equals("")) && (dt.length() < format.length())) {  
 35                     dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]",  
 36                             "0");  
 37                 }  
 38                 date = (Date) dateFormat.parse(dt);  
 39             } catch (Exception e) {  
 40             }  
 41             return date;  
 42         }  
 43       
 44         /** 
 45          * 功能描述:格式化日期 
 46          *  
 47          * @param dateStr 
 48          *            String 字符型日期:YYYY-MM-DD 格式 
 49          * @return Date 
 50          */  
 51         public static Date parseDate(String dateStr) {  
 52             return parseDate(dateStr, "yyyy/MM/dd");  
 53         }  
 54       
 55         /** 
 56          * 功能描述:格式化输出日期 
 57          *  
 58          * @param date 
 59          *            Date 日期 
 60          * @param format 
 61          *            String 格式 
 62          * @return 返回字符型日期 
 63          */  
 64         public static String format(Date date, String format) {  
 65             String result = "";  
 66             try {  
 67                 if (date != null) {  
 68                     dateFormat = new SimpleDateFormat(format);  
 69                     result = dateFormat.format(date);  
 70                 }  
 71             } catch (Exception e) {  
 72             }  
 73             return result;  
 74         }  
 75       
 76         /** 
 77          * 功能描述: 
 78          *  
 79          * @param date 
 80          *            Date 日期 
 81          * @return 
 82          */  
 83         public static String format(Date date) {  
 84             return format(date, "yyyy/MM/dd");  
 85         }  
 86       
 87         /** 
 88          * 功能描述:返回年份 
 89          *  
 90          * @param date 
 91          *            Date 日期 
 92          * @return 返回年份 
 93          */  
 94         public static int getYear(Date date) {  
 95             calendar = Calendar.getInstance();  
 96             calendar.setTime(date);  
 97             return calendar.get(Calendar.YEAR);  
 98         }  
 99       
100         /** 
101          * 功能描述:返回月份 
102          *  
103          * @param date 
104          *            Date 日期 
105          * @return 返回月份 
106          */  
107         public static int getMonth(Date date) {  
108             calendar = Calendar.getInstance();  
109             calendar.setTime(date);  
110             return calendar.get(Calendar.MONTH) + 1;  
111         }  
112       
113         /** 
114          * 功能描述:返回日份 
115          *  
116          * @param date 
117          *            Date 日期 
118          * @return 返回日份 
119          */  
120         public static int getDay(Date date) {  
121             calendar = Calendar.getInstance();  
122             calendar.setTime(date);  
123             return calendar.get(Calendar.DAY_OF_MONTH);  
124         }  
125       
126         /** 
127          * 功能描述:返回小时 
128          *  
129          * @param date 
130          *            日期 
131          * @return 返回小时 
132          */  
133         public static int getHour(Date date) {  
134             calendar = Calendar.getInstance();  
135             calendar.setTime(date);  
136             return calendar.get(Calendar.HOUR_OF_DAY);  
137         }  
138       
139         /** 
140          * 功能描述:返回分钟 
141          *  
142          * @param date 
143          *            日期 
144          * @return 返回分钟 
145          */  
146         public static int getMinute(Date date) {  
147             calendar = Calendar.getInstance();  
148             calendar.setTime(date);  
149             return calendar.get(Calendar.MINUTE);  
150         }  
151       
152         /** 
153          * 返回秒钟 
154          *  
155          * @param date 
156          *            Date 日期 
157          * @return 返回秒钟 
158          */  
159         public static int getSecond(Date date) {  
160             calendar = Calendar.getInstance();  
161             calendar.setTime(date);  
162             return calendar.get(Calendar.SECOND);  
163         }  
164       
165         /** 
166          * 功能描述:返回毫秒 
167          *  
168          * @param date 
169          *            日期 
170          * @return 返回毫秒 
171          */  
172         public static long getMillis(Date date) {  
173             calendar = Calendar.getInstance();  
174             calendar.setTime(date);  
175             return calendar.getTimeInMillis();  
176         }  
177       
178         /** 
179          * 功能描述:返回字符型日期 
180          *  
181          * @param date 
182          *            日期 
183          * @return 返回字符型日期 yyyy/MM/dd 格式 
184          */  
185         public static String getDate(Date date) {  
186             return format(date, "yyyy/MM/dd");  
187         }  
188       
189         /** 
190          * 功能描述:返回字符型时间 
191          *  
192          * @param date 
193          *            Date 日期 
194          * @return 返回字符型时间 HH:mm:ss 格式 
195          */  
196         public static String getTime(Date date) {  
197             return format(date, "HH:mm:ss");  
198         }  
199       
200         /** 
201          * 功能描述:返回字符型日期时间 
202          *  
203          * @param date 
204          *            Date 日期 
205          * @return 返回字符型日期时间 yyyy/MM/dd HH:mm:ss 格式 
206          */  
207         public static String getDateTime(Date date) {  
208             return format(date, "yyyy/MM/dd HH:mm:ss");  
209         }  
210       
211         /** 
212          * 功能描述:日期相加 
213          *  
214          * @param date 
215          *            Date 日期 
216          * @param day 
217          *            int 天数 
218          * @return 返回相加后的日期 
219          */  
220         public static Date addDate(Date date, int day) {  
221             calendar = Calendar.getInstance();  
222             long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;  
223             calendar.setTimeInMillis(millis);  
224             return calendar.getTime();  
225         }  
226       
227         /** 
228          * 功能描述:日期相减 
229          *  
230          * @param date 
231          *            Date 日期 
232          * @param date1 
233          *            Date 日期 
234          * @return 返回相减后的日期 
235          */  
236         public static int diffDate(Date date, Date date1) {  
237             return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));  
238         }  
239       
240         /** 
241          * 功能描述:取得指定月份的第一天 
242          *  
243          * @param strdate 
244          *            String 字符型日期 
245          * @return String yyyy-MM-dd 格式 
246          */  
247         public static String getMonthBegin(String strdate) {  
248             date = parseDate(strdate);  
249             return format(date, "yyyy-MM") + "-01";  
250         }  
251       
252         /** 
253          * 功能描述:取得指定月份的最后一天 
254          *  
255          * @param strdate 
256          *            String 字符型日期 
257          * @return String 日期字符串 yyyy-MM-dd格式 
258          */  
259         public static String getMonthEnd(String strdate) {  
260             date = parseDate(getMonthBegin(strdate));  
261             calendar = Calendar.getInstance();  
262             calendar.setTime(date);  
263             calendar.add(Calendar.MONTH, 1);  
264             calendar.add(Calendar.DAY_OF_YEAR, -1);  
265             return formatDate(calendar.getTime());  
266         }  
267       
268         /** 
269          * 功能描述:常用的格式化日期 
270          *  
271          * @param date 
272          *            Date 日期 
273          * @return String 日期字符串 yyyy-MM-dd格式 
274          */  
275         public static String formatDate(Date date) {  
276             return formatDateByFormat(date, "yyyy-MM-dd");  
277         }  
278       
279         /** 
280          * 功能描述:以指定的格式来格式化日期 
281          *  
282          * @param date 
283          *            Date 日期 
284          * @param format 
285          *            String 格式 
286          * @return String 日期字符串 
287          */  
288         public static String formatDateByFormat(Date date, String format) {  
289             String result = "";  
290             if (date != null) {  
291                 try {  
292                     SimpleDateFormat sdf = new SimpleDateFormat(format);  
293                     result = sdf.format(date);  
294                 } catch (Exception ex) {  
295                     ex.printStackTrace();  
296                 }  
297             }  
298             return result;  
299         }  
300       
301         public static void main(String[] args) {  
302             Date d = new Date();  
303             System.out.println(d.toString());  
304             System.out.println(formatDate(d).toString());  
305             System.out.println(getMonthBegin(formatDate(d).toString()));  
306             System.out.println(getMonthBegin("2008/07/19"));  
307             System.out.println(getMonthEnd("2008/07/19"));  
308             System.out.println(addDate(d,15).toString());  
309         }  
310       
311     }  

 

合理、科学地设计指令格式

标签:容量   cat   style   字符型   ret   main   res   The   seda   

原文地址:https://www.cnblogs.com/borter/p/9387592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!