Java工具类

  1 Java工具类
  2 1.Date时间
  3 Date date = new Date();
  4 System.out.println(date);
  5 System.out.println(date.toLocaleString());//过时的方法
  6 System.out.println(date.getDate());//当前的日期 一个月的第几天
  7 System.out.println(date.getDay());//星期几 返回值 (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) 表示一周中的某一天
  8 System.out.println(date.getHours());//当前小时
  9 System.out.println(date.getMinutes());//当前分钟
 10 System.out.println(date.getMonth());//0序的  11 从0开始
 11 System.out.println(date.getSeconds());
 12 System.out.println(date.getTime());//返回的是一个时间差;从现在和1970年0点0分0秒的时间差,毫秒表示
 13 System.out.println(date.getYear());//1900年和现在的一个时间差 以年为单位
 14 date.after(when)//date 是否在when 之后 返回boolean
 15 date.before(when)//date 是否在when 之前 返回boolean
 16 date.setYear(82);//从1900开始计时
 17 System.out.println(date.toLocaleString());
 18 
 19 2.DateFormat、SimpleDateFormat 格式化日期时间
 20 DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
 21 String dateStr = sdf.format(date);
 22 System.out.println(dateStr);
 23 DateFormat 这是一个抽象类,只能使用它的子类SimpleDateFormat 进行日期的格式化
 24 
 25 3.Calendar日历日期
 26 这是一个抽象类,不能new 只能通过它的静态的方法构造对象
 27 Calendar calendar = Calendar.getInstance();
 28 System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
 29 System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
 30 System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
 31 System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
 32 calendar.add(Calendar.DAY_OF_MONTH, 1);//增加了一天
 33 calendar.set(Calendar.DAY_OF_MONTH, 1);//第二种修改字段属性的方式
 34 System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
 35 System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
 36 System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
 37 
 38 System.out.println(calendar.getFirstDayOfWeek());
 39 System.out.println(calendar.getTimeInMillis());
 40 System.out.println(calendar.getMaximum(Calendar.DAY_OF_YEAR));
 41 
 42 作者:王立平的博客 
 43 来源:CSDN 
 44 原文:https://blog.csdn.net/qq_37131111/article/details/78824176 
 45 版权声明:本文为博主原创文章,转载请附上博文链接!
 46 
 47 1、编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。
 48 
 49 public class Exercise_09_01 {
 50     public static void main(String[] args) {
 51         //读取命令行数据,并进行类型转换
 52         int x = Integer.parseInt(args[0]);
 53         int y = Integer.parseInt(args[1]);
 54         int z = x+y;
 55         System.out.println(z);
 56         
 57     }
 58 }
 59 2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。
 60 public class Test {
 61 
 62     /**
 63      * @param args
 64      */
 65     public static void main(String[] args) {
 66         Scanner in = new Scanner(System.in);
 67         String s = in.next();
 68         int num = 0;
 69         for(int  i =0;i < s.length();i++){
 70             if(s.charAt(i)=='e'){
 71                 num++;
 72             }
 73         }
 74         System.out.println(num);
 75     }
 76 
 77 }
 78 
 79 3、生成十个0~100之间的随机数,放到数组中,然后排序输出。
 80 public class Test {
 81     public static void num(){
 82         
 83         int[] a = new int[8];
 84         for(int i = 0;i<a.length;i++){
 85             a[i] = (int)(Math.random()*101);
 86         }
 87         Arrays.sort(a);
 88         for(int i = 0;i<a.length;i++){
 89             System.out.println(a[i]);
 90         }
 91         
 92     }
 93     public static void main(String[] args) {
 94         num();
 95     }
 96 }
 97 
 98 
 99 4、    巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。
100 
101 public class TimeTest {
102     
103     public void getTime(int y,int m,int d,int h,int mi){
104         //获取Calendar实例
105         Calendar c = Calendar.getInstance();
106         //设置巴黎时间
107         c.set(y,m,d,h-7,mi);
108         
109         //输出巴黎时间
110         System.out.println("巴黎时间是"+c.get(Calendar.YEAR)+"年"+
111                    ((c.get(Calendar.MONTH))+1)+"月"+
112                    c.get(Calendar.DAY_OF_MONTH)+"日"+
113                    c.get(Calendar.HOUR_OF_DAY)+"点"+
114                    c.get(Calendar.MINUTE)+"分");
115         //设置纽约时间
116         c.set(y,m,d,h-12,mi);
117         //输出纽约时间
118         System.out.println("纽约时间是"+c.get(Calendar.YEAR)+"年"+
119                    ((c.get(Calendar.MONTH))+1)+"月"+
120                    c.get(Calendar.DAY_OF_MONTH)+"日"+
121                    c.get(Calendar.HOUR_OF_DAY)+"点"+
122                    c.get(Calendar.MINUTE)+"分");
123         
124     }
125 
126     public static void main(String[] args) {
127         TimeTest ex = new TimeTest();
128         //设置北京时间,月份是从0开始,数字2代表3月份
129         ex.getTime(2015, 2, 28, 16, 50);
130         
131     }
132 }
133 5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名
134 如果邮箱地址不合法则显示不合法的原因
135 提示:邮箱地址不合法的因素:
136 1)    邮箱地址中不包含@或。
137 2)    邮箱地址中含有多了@或。
138 3)    邮箱地址中。出现在@的前面
139 4)    用户名里有其他字符
140 实现步骤:
141 (1) 创建一个类,类名:mailtest
142 类图如下:
143  “”
144 (类名和方法名必须与要求一样。区分大小写)
145 public class Mailtest {
146     public static boolean testMail(){
147         Scanner in = new Scanner(System.in);
148         String s = in.next();
149         if(s.indexOf("@")==-1||s.indexOf(".")==-1){
150             
151             System.out.println("邮箱地址中不包含@或.");
152             return false;
153         }
154         
155         if(s.indexOf("@")!=s.lastIndexOf("@")||s.indexOf(".")!=s.lastIndexOf(".")){
156             System.out.println("邮箱地址中含有多了@或.");
157             return  false;
158         }
159         
160         if(s.indexOf("@")>s.lastIndexOf(".")){
161             System.out.println("邮箱地址中.出现在@的前面");
162             return false;
163         }
164         for(int i=0;i<s.indexOf("@");i++){
165             if( (s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z') ){
166                 
167             }else{
168                 System.out.println("用户名里有其他字符");
169                 return false;
170             }
171             
172         }
173         return true;
174     }
175 }
176 
177 public class Test {
178 
179 
180     /**
181      * @param args
182      */
183     public static void main(String[] args) {
184         // TODO Auto-generated method stub
185         
186         if(Mailtest.testMail()){
187             System.out.println("邮箱格式合法");
188         }else{
189             System.out.println("邮箱格式不合法");
190         }
191     }
192 
193 }
194 6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。
195 public class Test {
196     /**
197      * @param args
198      */
199     public static void main(String[] args) {
200         // TODO Auto-generated method stub
201         Scanner in = new Scanner(System.in);
202         String s = in.next();
203         String a = in.next();
204         
205         int num = 0;//i是循環的次數 
206         
207         for(int i = 0;i<s.length()-a.length();i=s.indexOf(a, i)+1){
208             if(s.indexOf(a, i)!=-1){
209                 num++;
210             }
211         }
212         System.out.println(num);
213     }
214 }
215 
216 7、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
217 public class Test {
218 
219     /**
220      * @param args
221      */
222     public static void main(String[] args) {
223         // TODO Auto-generated method stub
224         Scanner in = new Scanner(System.in);
225         String s = in.next();
226         int num=0,eng=0,china=0;
227         for(int i =0;i<s.length();i++){
228             if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
229                 num++;
230             }else if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){
231                 eng++;
232             }else if((19968 <= s.charAt(i) && s.charAt(i) <40623)) {
233                 china++;
234             }
235 
236         }
237         
238         System.out.println("数字是:"+num);
239         System.out.println("英文是:"+eng);
240         System.out.println("中文是:"+china);
241     }
242 
243 }
244 
245 
246 8. 有一种数叫回文数,正读和反读都一样,如12321便是一个回文数。编写一个程序,从命令行得到一个整数,判断该数是不是回文数。
247 public class Test {
248 
249     /**
250      * @param args
251      */
252     public static void main(String[] args) {
253         // TODO Auto-generated method stub
254         if(foo()){
255             System.out.println("是回数");
256         }else{
257             System.out.println("不是回数");
258         }
259     }
260     
261     public static boolean foo(){
262         Scanner in = new Scanner(System.in);
263         String s = in.next();
264         int j = s.length()-1;
265         for(int i=0;i<s.length()/2;i++){
266             
267             if(s.charAt(i)!=s.charAt(j)){
268                 
269                 return false;
270             }
271             j--;
272         }
273         
274         return true;
275     }
276 }

 

posted @ 2019-03-08 22:06  Pythons!  阅读(273)  评论(0编辑  收藏  举报