TimeExchange2.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package com.template.common.utils;
  2. import java.sql.Timestamp;
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. /**
  8. * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换
  9. * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错
  10. * @author
  11. *
  12. */
  13. public class TimeExchange2 {
  14. /**
  15. * String(yyyy-MM-dd HH:mm:ss) 转 Date
  16. *
  17. * @param time
  18. * @return
  19. * @throws ParseException
  20. */
  21. // String date = "2010/05/04 12:34:23";
  22. public static Date StringToDate(String time) throws ParseException {
  23. Date date = new Date();
  24. // 注意format的格式要与日期String的格式相匹配
  25. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  26. try {
  27. date = dateFormat.parse(time);
  28. System.out.println(date.toString());
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. return date;
  33. }
  34. public static Date StringToDate2(String time) throws ParseException {
  35. Date date = new Date();
  36. // 注意format的格式要与日期String的格式相匹配
  37. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  38. try {
  39. date = dateFormat.parse(time);
  40. System.out.println(date.toString());
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. return date;
  45. }
  46. /**
  47. * String(yyyy-MM-dd HH:mm:ss) 转 Date
  48. *
  49. * @param time
  50. * @return
  51. * @throws ParseException
  52. */
  53. // String date = "2010/05/04 12:34:23";
  54. public static Date StringToDate(String time, String formatStr) throws ParseException {
  55. Date date = new Date();
  56. // 注意format的格式要与日期String的格式相匹配
  57. DateFormat dateFormat = new SimpleDateFormat(formatStr);
  58. try {
  59. date = dateFormat.parse(time);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return date;
  64. }
  65. /**
  66. * Date转为String(yyyy-MM-dd HH:mm:ss)
  67. *
  68. * @param time
  69. * @return
  70. */
  71. public static String DateToString(Date time) {
  72. String dateStr = "";
  73. Date date = new Date();
  74. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
  75. try {
  76. dateStr = dateFormat.format(time);
  77. System.out.println(dateStr);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. return dateStr;
  82. }
  83. public static String ToSimpleMonth(Date time) {
  84. String dateStr = "";
  85. DateFormat dateFormat = new SimpleDateFormat("M");
  86. try {
  87. dateStr = dateFormat.format(time);
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. return dateStr;
  92. }
  93. /**
  94. * String(yyyy-MM-dd HH:mm:ss)转10位时间戳
  95. * @param time
  96. * @return
  97. */
  98. public static Integer StringToTimestamp(String time){
  99. int times = 0;
  100. try {
  101. times = (int) ((Timestamp.valueOf(time).getTime())/1000);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. if(times==0){
  106. System.out.println("String转10位时间戳失败");
  107. }
  108. return times;
  109. }
  110. /**
  111. * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss)
  112. * @param time
  113. * @return
  114. */
  115. public static String timestampToString(Integer time){
  116. //int转long时,先进行转型再进行计算,否则会是计算结束后在转型
  117. long temp = (long)time*1000;
  118. Timestamp ts = new Timestamp(temp);
  119. String tsStr = "";
  120. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  121. try {
  122. //方法一
  123. tsStr = dateFormat.format(ts);
  124. System.out.println(tsStr);
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. }
  128. return tsStr;
  129. }
  130. /**
  131. * 10位时间戳转Date
  132. * @param time
  133. * @return
  134. */
  135. public static Date TimestampToDate(Integer time){
  136. long temp = (long)time*1000;
  137. Timestamp ts = new Timestamp(temp);
  138. Date date = new Date();
  139. try {
  140. date = ts;
  141. //System.out.println(date);
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. }
  145. return date;
  146. }
  147. /**
  148. * Date类型转换为10位时间戳
  149. * @param time
  150. * @return
  151. */
  152. public static Integer DateToTimestamp(Date time){
  153. Timestamp ts = new Timestamp(time.getTime());
  154. return (int) ((ts.getTime())/1000);
  155. }
  156. // 当前时间减1小时
  157. public static String TimeDesH(String time) throws ParseException {
  158. Calendar nowTime2 = Calendar.getInstance();
  159. nowTime2.setTime(StringToDate(time));
  160. nowTime2.add(Calendar.HOUR, -1);
  161. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  162. return simpleDateFormat.format(nowTime2.getTime());
  163. }
  164. // 最近两天
  165. public static String getLastTwo() throws ParseException {
  166. Calendar nowTime2 = Calendar.getInstance();
  167. nowTime2.setTime(StringToDate(getTime()));
  168. nowTime2.add(Calendar.DATE, -2);
  169. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  170. return simpleDateFormat.format(nowTime2.getTime());
  171. }
  172. // 当前时间加2分钟
  173. public static String TimeRangeI10(String time,int m) throws ParseException {
  174. Calendar nowTime2 = Calendar.getInstance();
  175. nowTime2.setTime(StringToDate(time));
  176. nowTime2.add(Calendar.SECOND, m);//10分钟前的时间
  177. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  178. return simpleDateFormat.format(nowTime2.getTime());
  179. }
  180. // 当前时间加多少分钟
  181. public static String TimeRangeM(String time,int m) throws ParseException {
  182. Calendar nowTime2 = Calendar.getInstance();
  183. nowTime2.setTime(StringToDate2(time));
  184. nowTime2.add(Calendar.MINUTE, m);//10分钟前的时间
  185. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  186. return simpleDateFormat.format(nowTime2.getTime());
  187. }
  188. // 当前时间减5分钟
  189. public static String TimeRangeD(String time) throws ParseException {
  190. Calendar nowTime2 = Calendar.getInstance();
  191. nowTime2.setTime(StringToDate(time));
  192. nowTime2.add(Calendar.MINUTE, -300);//5分钟前的时间
  193. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  194. return simpleDateFormat.format(nowTime2.getTime());
  195. }
  196. // 获取当前日期
  197. public static String getDateStr(){
  198. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
  199. return sp.format(new Date());
  200. }
  201. // 获取当前日期
  202. public static String getDate(){
  203. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  204. return sp.format(new Date());
  205. }
  206. // 获取前天
  207. public static String getQianDay() throws ParseException {
  208. Calendar nowTime2 = Calendar.getInstance();
  209. nowTime2.setTime(StringToDate(getTime()));
  210. nowTime2.add(Calendar.DATE, -5);//5分钟前的时间
  211. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  212. return simpleDateFormat.format(nowTime2.getTime());
  213. }
  214. // 获取明天
  215. public static String getTomorrow() throws ParseException {
  216. Calendar nowTime2 = Calendar.getInstance();
  217. nowTime2.setTime(StringToDate(getTime()));
  218. nowTime2.add(Calendar.DATE, 1);//5分钟前的时间
  219. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  220. return simpleDateFormat.format(nowTime2.getTime());
  221. }
  222. // 获取昨天
  223. public static String getYesturday() {
  224. try {
  225. Calendar nowTime2 = Calendar.getInstance();
  226. nowTime2.setTime(StringToDate(getTime()));
  227. nowTime2.add(Calendar.DATE, -1);
  228. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  229. return simpleDateFormat.format(nowTime2.getTime());
  230. } catch (ParseException e) {
  231. throw new RuntimeException(e);
  232. }
  233. }
  234. // 获取最近7个月
  235. public static List<String> getLastSevenMonth() throws ParseException {
  236. List<String> ls = new ArrayList<>();
  237. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
  238. for (int i = -6; i <= 0; i++) {
  239. Calendar nowTime2 = Calendar.getInstance();
  240. nowTime2.setTime(StringToDate(getTime()));
  241. nowTime2.add(Calendar.MONTH, i);//5分钟前的时间
  242. ls.add(simpleDateFormat.format(nowTime2.getTime()));
  243. }
  244. return ls;
  245. }
  246. public static String getTomorrowTime() throws ParseException {
  247. Calendar nowTime2 = Calendar.getInstance();
  248. nowTime2.setTime(StringToDate(getTime()));
  249. nowTime2.add(Calendar.DATE, 1);//5分钟前的时间
  250. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  251. return simpleDateFormat.format(nowTime2.getTime());
  252. }
  253. public static String getWeek(String sdate) throws ParseException {
  254. // 再转换为时间
  255. Date date = StringToDate(sdate,"yyyy-MM-dd");
  256. Calendar c = Calendar.getInstance();
  257. c.setTime(date);
  258. // int hour=c.get(Calendar.DAY_OF_WEEK);
  259. // hour中存的就是星期几了,其范围 1~7
  260. // 1=星期日 7=星期六,其他类推
  261. return new SimpleDateFormat("EEEE").format(c.getTime());
  262. }
  263. // 今天星期几
  264. public static String getWeek() throws ParseException {
  265. String[] weeks = {"7","1","2","3","4","5","6"};
  266. Calendar cal = Calendar.getInstance();
  267. cal.setTime(StringToDate(getTime()));
  268. int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
  269. if(week_index<0){
  270. week_index = 0;
  271. }
  272. return weeks[week_index];
  273. }
  274. // 明天星期几
  275. public static String getTomorrowWeek() throws ParseException {
  276. String[] weeks = {"7","1","2","3","4","5","6"};
  277. Calendar cal = Calendar.getInstance();
  278. cal.setTime(StringToDate(getTomorrowTime()));
  279. int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
  280. if(week_index<0){
  281. week_index = 0;
  282. }
  283. return weeks[week_index];
  284. }
  285. // 获取当前时间
  286. public static String getTime(){
  287. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  288. return sp.format(new Date());
  289. }
  290. public static String getOnlyMM(){
  291. SimpleDateFormat sp = new SimpleDateFormat("HH:mm");
  292. return sp.format(new Date());
  293. }
  294. public static String getOnlyDesMM() throws ParseException {
  295. Calendar nowTime2 = Calendar.getInstance();
  296. nowTime2.setTime(StringToDate(getTime()));
  297. nowTime2.add(Calendar.MINUTE, -5);//5分钟前的时间
  298. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  299. return simpleDateFormat.format(nowTime2.getTime());
  300. }
  301. public static String getYear(){
  302. SimpleDateFormat sp = new SimpleDateFormat("yyyy");
  303. return sp.format(new Date());
  304. }
  305. public static String getMonth(){
  306. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM");
  307. return sp.format(new Date());
  308. }
  309. // 获取当前时间
  310. public static String getOnlyTime(){
  311. SimpleDateFormat sp = new SimpleDateFormat("HH:mm:ss");
  312. return sp.format(new Date());
  313. }
  314. /**
  315. * 计算两个日期的时间差
  316. * @param time1
  317. * @param time2
  318. * @return
  319. */
  320. public static double getTimeDifference(String time1, String time2) {
  321. SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  322. long t1 = 0L;
  323. long t2 = 0L;
  324. try {
  325. t1 = timeformat.parse(time1).getTime();
  326. } catch (ParseException e) {
  327. // TODO Auto-generated catch block
  328. e.printStackTrace();
  329. }
  330. try {
  331. t2 = timeformat.parse(time2).getTime();
  332. } catch (ParseException e) {
  333. // TODO Auto-generated catch block
  334. e.printStackTrace();
  335. }
  336. //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
  337. double hours=(double) ((t2 - t1)/3600000);
  338. double minutes=(double) (((t2 - t1)/1000-hours*3600)/60/60);
  339. return hours+minutes;
  340. }
  341. public static double getOnlyTimeDifference(String time1, String time2) {
  342. SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
  343. long t1 = 0L;
  344. long t2 = 0L;
  345. try {
  346. t1 = timeformat.parse(time1).getTime();
  347. } catch (ParseException e) {
  348. // TODO Auto-generated catch block
  349. e.printStackTrace();
  350. }
  351. try {
  352. t2 = timeformat.parse(time2).getTime();
  353. } catch (ParseException e) {
  354. // TODO Auto-generated catch block
  355. e.printStackTrace();
  356. }
  357. //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
  358. double hours=(double) ((t2 - t1)/3600000);
  359. double minutes=(double) (((t2 - t1)/1000-hours*3600)/60/60);
  360. return hours+minutes;
  361. }
  362. public static double getDiff(String str1, String str2){
  363. return str2.compareTo(str1);
  364. }
  365. /**
  366. * String 转 Date
  367. * @param time 时间
  368. * @param formatStr 自定义时间格式
  369. * @return
  370. * @throws ParseException
  371. */
  372. public static Date ShortStringToDate(String time, String formatStr) throws ParseException {
  373. Date date = new Date();
  374. // 注意format的格式要与日期String的格式相匹配
  375. DateFormat dateFormat = new SimpleDateFormat(formatStr);
  376. try {
  377. date = dateFormat.parse(time);
  378. System.out.println(date.toString());
  379. } catch (Exception e) {
  380. e.printStackTrace();
  381. }
  382. return date;
  383. }
  384. /**
  385. * 获取指定月份有多少天
  386. *
  387. * @param month
  388. * @return
  389. */
  390. public static int getMonthDays(String date, int month) {
  391. int year = Integer.valueOf(date.substring(0, 4));
  392. int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  393. int day = arr[month - 1];//天数对应=数组-1
  394. if (month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  395. day = 29;
  396. }
  397. return day;
  398. }
  399. /**
  400. * Date转为String
  401. * @param time 时间
  402. * @param FormatStr 自定义时间格式
  403. * @return
  404. */
  405. public static String DateToString(Date time, String FormatStr) {
  406. String dateStr = "";
  407. DateFormat dateFormat = new SimpleDateFormat(FormatStr);
  408. try {
  409. dateStr = dateFormat.format(time);
  410. System.out.println(dateStr);
  411. } catch (Exception e) {
  412. e.printStackTrace();
  413. }
  414. return dateStr;
  415. }
  416. /**
  417. * 比较时间1是否小于时间2
  418. * 如果时间1小于时间2,接口返回true
  419. * 如果时间1大于时间2,接口返回false
  420. * @param dateOne 时间1
  421. * @param dateTwo 时间2
  422. * @return
  423. * @throws ParseException
  424. */
  425. public static boolean CompareDate(String dateOne, String dateTwo) throws ParseException {
  426. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  427. Date sd1=df.parse(dateOne);
  428. Date sd2=df.parse(dateTwo);
  429. return sd1.before(sd2);
  430. }
  431. /**
  432. * 比较时间1是否小于时间2
  433. * 如果时间1小于时间2,接口返回true
  434. * 如果时间1大于时间2,接口返回false
  435. *
  436. * @param dateOne 时间1
  437. * @param dateTwo 时间2
  438. * @param Forma 时间格式
  439. * @return
  440. * @throws ParseException
  441. */
  442. public static boolean CompareDate(String dateOne, String dateTwo, String Forma) throws ParseException {
  443. SimpleDateFormat df = new SimpleDateFormat(Forma);
  444. Date sd1 = df.parse(dateOne);
  445. Date sd2 = df.parse(dateTwo);
  446. return sd1.before(sd2);
  447. }
  448. /**
  449. * 时间加减天数
  450. *
  451. * @param time 时间
  452. * @param amount 天数 负的为减多少天 正的为加多少天
  453. * @return
  454. * @throws ParseException
  455. */
  456. public static String TimeDesD(String time, int amount) throws ParseException {
  457. Calendar nowTime2 = Calendar.getInstance();
  458. nowTime2.setTime(StringToDate(time, "yyyy-MM-dd"));
  459. nowTime2.add(Calendar.DATE, amount);
  460. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  461. return simpleDateFormat.format(nowTime2.getTime());
  462. }
  463. /**
  464. * 相差的天数
  465. */
  466. public static int daysBetween(String smdate, String bdate) throws ParseException {
  467. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  468. Calendar cal = Calendar.getInstance();
  469. cal.setTime(sdf.parse(smdate));
  470. long time1 = cal.getTimeInMillis();
  471. cal.setTime(sdf.parse(bdate));
  472. long time2 = cal.getTimeInMillis();
  473. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  474. return Integer.parseInt(String.valueOf(between_days));
  475. }
  476. /**
  477. * 获取一周的开始时间和结束时间
  478. * 获取本周星期一作为一周的第一天的起始时间和结束时间
  479. *
  480. * @return 返回的数据中第一个是开始时间 第二个是结束时间
  481. */
  482. public static String[] getCurrentWeekTimeFrame() {
  483. Calendar calendar = Calendar.getInstance();
  484. calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
  485. //start of the week
  486. if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  487. calendar.add(Calendar.DAY_OF_YEAR,-1);
  488. }
  489. calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
  490. //给0的时候查不出数据
  491. // calendar.set(Calendar.HOUR_OF_DAY, 0);
  492. // calendar.set(Calendar.MINUTE, 0);
  493. // calendar.set(Calendar.SECOND, 0);
  494. // calendar.set(Calendar.MILLISECOND, 0);
  495. String startTime = DateToString(calendar.getTime(), "yyyy-MM-dd");
  496. //end of the week
  497. calendar.add(Calendar.DAY_OF_WEEK, 6);
  498. calendar.set(Calendar.HOUR_OF_DAY, 23);
  499. calendar.set(Calendar.MINUTE, 59);
  500. calendar.set(Calendar.SECOND, 59);
  501. calendar.set(Calendar.MILLISECOND, 999);
  502. String endTime = DateToString(calendar.getTime());
  503. return new String[]{startTime, endTime};
  504. }
  505. /**
  506. * 获取指定月份的第一天和最后一天
  507. * @param DateStr 指定月份
  508. * @return 返回的数据中第一个是开始时间 第二个是结束时间
  509. */
  510. public static String[] getCurrentMonthTimeFrame(String DateStr) {
  511. Calendar c = Calendar.getInstance();//获取Calendar实例
  512. c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4)));
  513. c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1);
  514. c.set(Calendar.DAY_OF_MONTH, 1);
  515. String startDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  516. int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  517. c.set(Calendar.DAY_OF_MONTH, lastDay);
  518. String endDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  519. return new String[]{startDate, endDate};
  520. }
  521. public static List<String> getHighTime(){
  522. String year = getYear();
  523. return Arrays.asList(year+"-"+"08-30",year+"-"+"08-31",year+"-"+"09-01",year+"-"+"09-02",year+"-"+"09-03",year+"-"+"09-04");
  524. }
  525. public static List<String> getDays(String startTime, String endTime) {
  526. // 返回的日期集合
  527. List<String> days = new ArrayList<String>();
  528. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  529. try {
  530. Date start = dateFormat.parse(startTime);
  531. Date end = dateFormat.parse(endTime);
  532. Calendar tempStart = Calendar.getInstance();
  533. tempStart.setTime(start);
  534. Calendar tempEnd = Calendar.getInstance();
  535. tempEnd.setTime(end);
  536. tempEnd.add(Calendar.DATE, 0);// 日期加1(包含结束)
  537. while (tempStart.before(tempEnd)) {
  538. days.add(dateFormat.format(tempStart.getTime()));
  539. tempStart.add(Calendar.DAY_OF_YEAR, 1);
  540. }
  541. } catch (ParseException e) {
  542. e.printStackTrace();
  543. }
  544. return days;
  545. }
  546. public static void main(String[] args) throws ParseException {
  547. char a = 'A';
  548. char b = 'B';
  549. int result = b -a;
  550. String s = "ZY";
  551. int ans = 0;
  552. for(int i=0;i<s.length();i++) {
  553. int num = s.charAt(i) - 'A' + 1;
  554. ans = ans * 26 + num;
  555. }
  556. System.out.println(ans);
  557. }
  558. }