1. Java时间类

对时间类进行转换解析

1. 将时间字符串转换为时间戳

  1. public static void main(String[] args) {
  2. String time = "20190619000000";
  3. SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMddHHmmss");
  4. try {
  5. Date tmpdate = ft.parse(time);
  6. } catch (ParseException e) {
  7. e.printStackTrace();
  8. }
  9. }

2.统计两个时间区间的月的个数

  1. private List<Integer> getMonsByTimeRange(String startTime, String endTime) throws ParseException {
  2. List<Integer> monLit = new ArrayList<>();
  3. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//格式化为年月
  4. Calendar min = Calendar.getInstance();
  5. Calendar max = Calendar.getInstance();
  6. min.setTime(sdf.parse(startTime));
  7. min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  8. max.setTime(sdf.parse(endTime));
  9. max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  10. Calendar curr = min;
  11. while (curr.before(max)) {
  12. int mon = curr.getTime().getMonth();
  13. monLit.add(mon);
  14. curr.add(Calendar.MONTH, 1);
  15. }
  16. return monLit;
  17. }

2.Java文件操作类

1.遍历某个路径下的文件列表

  1. private List<String> GetFiles(String path) {
  2. List<String> tmpfilelist = new ArrayList<>();
  3. File tmpfilehandler = new File(path);
  4. for( File tmpfile: tmpfilehandler.listFiles()) {
  5. if ( tmpfile.isFile() ) {
  6. tmpfilelist.add(tmpfile.toString());
  7. }
  8. }
  9. return tmpfilelist;
  10. }

2.删除某个文件或某个目录下的所有文件

  1. public static boolean DeleteFile(String filepath) {
  2. if (StringUtils.isEmpty(filepath) ) {
  3. return false;
  4. }
  5. File tmpfilehandler = new File(filepath);
  6. if ( !tmpfilehandler.exists() ) {
  7. return false;
  8. }
  9. if ( tmpfilehandler.isFile() ) {
  10. return tmpfilehandler.delete();
  11. } else {
  12. String []filenames = tmpfilehandler.list();
  13. for(String f: filenames) {
  14. DeleteFile(f);
  15. }
  16. return tmpfilehandler.delete();
  17. }
  18. }

3. 列出某个目录下的所有目录

  1. public static List<String> GetDirs(String path) {
  2. List<String> tmpdirlist = new ArrayList<>();
  3. File currentdir = new File(path);
  4. for( File tmpfile: currentdir.listFiles()) {
  5. if ( tmpfile.isDirectory() ) {
  6. tmpdirlist.add(tmpfile.toString());
  7. }
  8. }
  9. return tmpdirlist;
  10. }

3.Java数值操作类

1. 将平时用到的数值操作类,集中管理,用于以后扩展

  1. import java.math.BigDecimal;
  2. /**
  3. * description
  4. *
  5. * @author ${user}
  6. * @Time 2019-06-23
  7. */
  8. public class DecimalUtils {
  9. # 01.实现对指定浮点数范围
  10. public static float setScale(float value, int scala) {
  11. BigDecimal bigDecimal = new BigDecimal((double)value);
  12. bigDecimal = bigDecimal.setScale(scala, 4);
  13. return bigDecimal.floatValue();
  14. }
  15. }

4.JAVA基础-Collections工具类

1.排序操作

  1. Collections提供以下方法对List进行排序操作
  2. void reverse(List list):反转
  3. void shuffle(List list),随机排序
  4. void sort(List list),按自然排序的升序排序
  5. void sort(List list, Comparator c);定制排序,由Comparator控制排序逻辑
  6. void swap(List list, int i , int j),交换两个索引位置的元素
  7. #list--这是一个不可修改视图是要返回的列表中
  8. public static <T> List<T> unmodifiableList(List<? extends T> list)
  9. void rotate(List list, int distance),旋转。当distance为正数时,将listdistance个元素整体移到前面。当distance为负数时,将 list的前distance个元素整体移到后面。

下面简单演示Collections操作List

  1. package collection;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. public class CollectionsTest {
  6. public static void main(String[] args) {
  7. ArrayList nums = new ArrayList();
  8. nums.add(8);
  9. nums.add(-3);
  10. nums.add(2);
  11. nums.add(9);
  12. nums.add(-2);
  13. System.out.println(nums);
  14. Collections.reverse(nums);
  15. System.out.println(nums);
  16. Collections.sort(nums);
  17. System.out.println(nums);
  18. Collections.shuffle(nums);
  19. System.out.println(nums);
  20. //下面只是为了演示定制排序的用法,将int类型转成string进行比较
  21. Collections.sort(nums, new Comparator() {
  22. @Override
  23. public int compare(Object o1, Object o2) {
  24. // TODO Auto-generated method stub
  25. String s1 = String.valueOf(o1);
  26. String s2 = String.valueOf(o2);
  27. return s1.compareTo(s2);
  28. }
  29. });
  30. System.out.println(nums);
  31. }
  32. }

2. 查找,替换操作

  1. int binarySearch(List list, Object key), List进行二分查找,返回索引,注意List必须是有序的
  2. int max(Collection coll),根据元素的自然顺序,返回最大的元素。 类比int min(Collection coll)
  3. int max(Collection coll, Comparator c),根据定制排序,返回最大元素,排序规则由Comparatator类控制。类比int min(Collection coll, Comparator c)
  4. void fill(List list, Object obj),用元素obj填充list中所有元素
  5. int frequency(Collection c, Object o),统计元素出现次数
  6. int indexOfSubList(List list, List target), 统计targelist中第一次出现的索引,找不到则返回-1,类比int lastIndexOfSubList(List source, list target).
  7. boolean replaceAll(List list, Object oldVal, Object newVal), 用新元素替换旧元素。

下面示范简单用法

  1. package collection.collections;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. public class CollectionsTest {
  5. public static void main(String[] args) {
  6. ArrayList num = new ArrayList();
  7. num.add(3);
  8. num.add(-1);
  9. num.add(-5);
  10. num.add(10);
  11. System.out.println(num);
  12. System.out.println(Collections.max(num));
  13. System.out.println(Collections.min(num));
  14. Collections.replaceAll(num, -1, -7);
  15. System.out.println(Collections.frequency(num, 3));
  16. Collections.sort(num);
  17. System.out.println(Collections.binarySearch(num, -5));
  18. }
  19. }

3. 同步控制

  1. Collections中几乎对每个集合都定义了同步控制方法,
  2. 例如 SynchronizedList(), SynchronizedSet()等方法,
  3. 来将集合包装成线程安全的集合。下面是Collections将普通集合包装成线程安全集合的用法,

例子:

  1. package collection.collections;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. public class SynchronizedTest {
  11. public static void main(String[] args) {
  12. Collection c = Collections.synchronizedCollection(new ArrayList());
  13. List list = Collections.synchronizedList(new ArrayList());
  14. Set s = Collections.synchronizedSet(new HashSet());
  15. Map m = Collections.synchronizedMap(new HashMap());
  16. }
  17. }

5. 常用类库:java.lang.ThreadLocal

1.使用场景

  1. ThreadLocalsynchronized都是为了解决多线程中相同变量的访问冲突问题。
  2. synchronized保证同一时刻只有一个线程对共享变量进行操作,synchronized是时间换空间的体现。
  3. ThreadLocal使变量在每个线程中都有独立拷贝的共享变量,不会出现一个线程读取变量时而被另一个线程修改的现象。
  4. ThreadLocal是编程中空间换时间的体现

2.ThreadLocal的内部结构

  1. 1、每个Thread线程内部都有一个Map
  2. 2Map里面存储线程本地对象(key)和线程的变量副本(value)。
  3. 3、每个Thread线程内部都有一个Map
  4. 4Thread对于不同的线程,每次获取副本值时,别的线程并不能获取到当前线程的副本值,形成了副本的隔离,
  5. 互不干扰。

3.ThreadLocal核心方法

  1. get()方法用于获取当前线程的副本变量值。
  2. set()方法用于保存当前线程的副本变量值。
  3. initialValue()为当前线程初始副本变量值。
  4. remove()方法移除当前前程的副本变量值。

4.ThreadLocal使用实例

  1. //包含业务唯一标识的类
  2. public class Context {
  3. private String transactionId;
  4. public String getTransactionId() {
  5. return transactionId;
  6. }
  7. public void setTransactionId(String transactionId) {
  8. this.transactionId = transactionId;
  9. }
  10. }
  1. // 其中引用了Context类
  2. public class MyThreadLocal {
  3. private static final ThreadLocal<Context> userThreadLocal = new ThreadLocal<Context>();
  4. public static void set(Context user){
  5. userThreadLocal.set(user);
  6. }
  7. public static void unset(){
  8. userThreadLocal.remove();
  9. }
  10. public static Context get(){
  11. return userThreadLocal.get();
  12. }
  13. }
  1. //ThreadLocalDemo.java。生成并将业务标识设置到ThreadLocal中然后在业务方法中调用
  2. public class ThreadLocalDemo implements Runnable{
  3. private static AtomicInteger ai = new AtomicInteger(0);
  4. public void run() {
  5. Context context = new Context();
  6. context.setTransactionId(getName());
  7. //设置线程变量
  8. MyThreadLocal.set(context);
  9. System.out.println("request["+Thread.currentThread().getName()+"]:"+context.getTransactionId());
  10. new BusinessService().businessMethod();
  11. MyThreadLocal.unset();
  12. }
  13. private String getName() {
  14. return ai.getAndIncrement()+"";
  15. }
  16. public static void main(String[] args) {
  17. ThreadLocalDemo tld = new ThreadLocalDemo();
  18. new Thread(tld).start();
  19. new Thread(tld).start();
  20. }
  21. }
  1. public class BusinessService {
  2. public void businessMethod() {
  3. Context context = MyThreadLocal.get();
  4. System.out.println("service["+Thread.currentThread().getName()+"]:"+context.getTransactionId());
  5. }
  6. }

5.ThreadLocal使用总结

  1. 1、每个ThreadLocal只能保存一个变量副本,如果想要上线一个线程能够保存多个副本以上,就需要创建多个ThreadLocal
  2. 2ThreadLocal内部的ThreadLocalMap键为弱引用,会有内存泄漏的风险。
  3. 3ThreadLocal适用于无状态,副本变量独立后不影响业务逻辑的高并发场景。如果如果业务逻辑强依赖于副本变量,则不适合用ThreadLocal解决,需要另寻解决方案。

6. java8新特性-Base64

  1. import java.util.Base64;
  2. import java.util.UUID;
  3. import java.io.UnsupportedEncodingException;
  4. public class Java8Tester {
  5. public static void main(String args[]){
  6. try {
  7. // 使用基本编码
  8. String base64encodedString = Base64.getEncoder().encodeToString("runoob?java8".getBytes("utf-8"));
  9. System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);
  10. // 解码
  11. byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
  12. System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
  13. base64encodedString = Base64.getUrlEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
  14. System.out.println("Base64 编码字符串 (URL) :" + base64encodedString);
  15. StringBuilder stringBuilder = new StringBuilder();
  16. for (int i = 0; i < 10; ++i) {
  17. stringBuilder.append(UUID.randomUUID().toString());
  18. }
  19. byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
  20. String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
  21. System.out.println("Base64 编码字符串 (MIME) :" + mimeEncodedString);
  22. }catch(UnsupportedEncodingException e){
  23. System.out.println("Error :" + e.getMessage());
  24. }
  25. }
  26. }

7.使用Kryo将数据尽心序列化到本地磁盘,反序列使用

Kryo工具类使用

  1. /**
  2. * 序列化至硬盘
  3. * @param filePath 序列化路径
  4. * @param list 带序列号列表
  5. * @param <T> 泛型
  6. */
  7. public static <T> void serializeObject(String filePath, List<T> list) {
  8. if (CollectionUtils.isEmpty(list)) {
  9. LOGGER.warn("没有要待序列化的东西");
  10. }
  11. Kryo kryo = new Kryo();
  12. kryo.setReferences(false);
  13. kryo.setRegistrationRequired(false);
  14. kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
  15. Output output = null;
  16. try {
  17. Path path = Paths.get(filePath);
  18. if (Files.notExists(path)) {
  19. Files.createFile(path);
  20. }
  21. output = new Output(new FileOutputStream(filePath));
  22. T t = list.get(0);
  23. kryo.register(t.getClass());
  24. kryo.writeObject(output, t);
  25. for (int i = 1; i < list.size(); i++) {
  26. T instance = list.get(i);
  27. kryo.writeObject(output, instance);
  28. }
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. LOGGER.warn(e.getMessage(), e.fillInStackTrace());
  32. } finally {
  33. if (output != null) {
  34. output.close();
  35. }
  36. }
  37. }
  38. /**
  39. * 反序列化至类
  40. * @param filePath 序列化文件路径
  41. * @param clazz 反序列化class类名
  42. * @param <T> 泛型
  43. * @return 反序列化后的类
  44. */
  45. public static <T>List<T> deserializeObject(String filePath,Class<T> clazz) {
  46. Path path = Paths.get(filePath);
  47. if (Files.notExists(path) || Files.isDirectory(path)) {
  48. LOGGER.warn("反序列化文件不存在,或不是文件");
  49. return null;
  50. }
  51. if(clazz == null){
  52. LOGGER.warn("反序列化的类为null");
  53. return null;
  54. }
  55. List<T> list = Lists.newArrayList();
  56. Kryo kryo = new Kryo();
  57. kryo.setReferences(false);
  58. kryo.setRegistrationRequired(false);
  59. kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
  60. Input input = null;
  61. try {
  62. T t;
  63. input = new Input(new FileInputStream(filePath),10240);
  64. while (input.canReadInt()){
  65. t = kryo.readObject(input,clazz);
  66. list.add(t);
  67. }
  68. }catch (IOException e){
  69. e.printStackTrace();
  70. LOGGER.error(e.getMessage(),e.fillInStackTrace());
  71. }finally {
  72. if(input != null){
  73. input.close();
  74. }
  75. }
  76. return list;
  77. }

8.List中的sort应用

  1. List<Map<String, Object>> list = stationDrps.get(s);
  2. float pv = 0;
  3. float av = 0;
  4. if (list.size() == 1) {
  5. // logger.info(s+" drp: " + list.get(0).get("PVALUE").toString());
  6. String pvalue = list.get(0).get("PVALUE").toString();
  7. String absval = list.get(0).get("ABSVAL").toString();
  8. pv = Float.parseFloat(pvalue);
  9. av = Float.parseFloat(absval);
  10. }
  11. list.sort(new Comparator<Map<String, Object>>() {
  12. @Override
  13. public int compare(Map<String, Object> o1, Map<String, Object> o2) {
  14. String s1 = o1.get("ABSVAL").toString();
  15. String s2 = o2.get("ABSVAL").toString();
  16. float f1 = Float.parseFloat(s1);
  17. float f2 = Float.parseFloat(s2);
  18. if (f1 == f2) {
  19. return 0;
  20. }
  21. return f1 > f2 ? 1 : -1;
  22. }
  23. });

9.正则匹配

  1. public static void Test1() {
  2. String filename = "Z_P_LPD_C_BABJ_20190619005201_2019_06_19.txt";
  3. String pattern = "Z_P_LPD_C_BABJ_\\d{14}_\\d{4}(_\\d{2}){2}(.txt|.TXT)";
  4. Pattern r = Pattern.compile(pattern);
  5. boolean flag = r.matcher(filename).matches();
  6. System.out.println(flag);
  7. }

10. Java 文件监控,实时监控文件加载之Commons-io

  1. package wy.util.filemonitor;
  2. import java.io.File;
  3. import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
  4. import org.apache.commons.io.monitor.FileAlterationObserver;
  5. import org.apache.log4j.Logger;
  6. /**
  7. * 文件变化监听器
  8. *
  9. * 在Apache的Commons-IO中有关于文件的监控功能的代码. 文件监控的原理如下:
  10. * 由文件监控类FileAlterationMonitor中的线程不停的扫描文件观察器FileAlterationObserver,
  11. * 如果有文件的变化,则根据相关的文件比较器,判断文件时新增,还是删除,还是更改。(默认为1000毫秒执行一次扫描)
  12. *
  13. * @author wy
  14. *
  15. */
  16. public class FileListener extends FileAlterationListenerAdaptor {
  17. private Logger log = Logger.getLogger(FileListener.class);
  18. /**
  19. * 文件创建执行
  20. */
  21. @Override
  22. public void onFileCreate(File file) {
  23. log.info("[新建]:" + file.getAbsolutePath());
  24. }
  25. /**
  26. * 文件创建修改
  27. */
  28. @Override
  29. public void onFileChange(File file) {
  30. log.info("[修改]:" + file.getAbsolutePath());
  31. }
  32. /**
  33. * 文件删除
  34. */
  35. @Override
  36. public void onFileDelete(File file) {
  37. log.info("[删除]:" + file.getAbsolutePath());
  38. }
  39. /**
  40. * 目录创建
  41. */
  42. @Override
  43. public void onDirectoryCreate(File directory) {
  44. log.info("[新建]:" + directory.getAbsolutePath());
  45. }
  46. /**
  47. * 目录修改
  48. */
  49. @Override
  50. public void onDirectoryChange(File directory) {
  51. log.info("[修改]:" + directory.getAbsolutePath());
  52. }
  53. /**
  54. * 目录删除
  55. */
  56. @Override
  57. public void onDirectoryDelete(File directory) {
  58. log.info("[删除]:" + directory.getAbsolutePath());
  59. }
  60. @Override
  61. public void onStart(FileAlterationObserver observer) {
  62. // TODO Auto-generated method stub
  63. super.onStart(observer);
  64. }
  65. @Override
  66. public void onStop(FileAlterationObserver observer) {
  67. // TODO Auto-generated method stub
  68. super.onStop(observer);
  69. }
  70. }

Java

  1. package wy.util.filemonitor;
  2. import java.util.concurrent.TimeUnit;
  3. import org.apache.commons.io.filefilter.FileFilterUtils;
  4. import org.apache.commons.io.monitor.FileAlterationMonitor;
  5. import org.apache.commons.io.monitor.FileAlterationObserver;
  6. /**
  7. * 文件监控测试
  8. *
  9. * 在Apache的Commons-IO中有关于文件的监控功能的代码. 文件监控的原理如下:
  10. * 由文件监控类FileAlterationMonitor中的线程不停的扫描文件观察器FileAlterationObserver,
  11. * 如果有文件的变化,则根据相关的文件比较器,判断文件时新增,还是删除,还是更改。(默认为1000毫秒执行一次扫描)
  12. *
  13. * @author wy
  14. *
  15. */
  16. public class FileMonitorTest {
  17. /**
  18. * @param args
  19. */
  20. public static void main(String[] args) throws Exception{
  21. // 监控目录
  22. String rootDir = "F:\\Resume";
  23. // 轮询间隔 5 秒
  24. long interval = TimeUnit.SECONDS.toMillis(5);
  25. // 创建一个文件观察器用于处理文件的格式
  26. FileAlterationObserver _observer = new FileAlterationObserver(
  27. rootDir,
  28. FileFilterUtils.and(
  29. FileFilterUtils.fileFileFilter(),
  30. FileFilterUtils.suffixFileFilter(".txt")), //过滤文件格式
  31. null);
  32. FileAlterationObserver observer = new FileAlterationObserver(rootDir);
  33. observer.addListener(new FileListener()); //设置文件变化监听器
  34. //创建文件变化监听器
  35. FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
  36. // 开始监控
  37. monitor.start();
  38. }
  39. }

11.java 读取配置文件API

读取配置文件属性类

  1. package com.hfqx.decodelpd.utils;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.Properties;
  6. /**
  7. * description
  8. *
  9. * @author ${user}
  10. * @Time 2019-04-04
  11. */
  12. public class PropertiesUtils {
  13. private final static String CONFIGPATH="config\\decode.properties";
  14. private static Properties properties;
  15. {
  16. properties = new Properties();
  17. try {
  18. // properties.load(new InputStreamReader(PropertiesUtils.class.getClassLoader().getResourceAsStream(CONFIGPATH),
  19. // "UTF-8"));
  20. properties.load(new InputStreamReader(new FileInputStream(CONFIGPATH),
  21. "UTF-8"));
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. public String getProperty(String key) {
  27. String value = null;
  28. value = properties.getProperty(key);
  29. return value;
  30. }
  31. public static void TestGetPro(String key) {
  32. PropertiesUtils propertiesUtils = new PropertiesUtils();
  33. String value = propertiesUtils.getProperty(key);
  34. System.out.println(value);
  35. }
  36. public static void main(String[] args) {
  37. TestGetPro("DATABASE_TYPE");
  38. }
  39. }

12. Java解析气象温度 -项目中的应用

按行读取文件内容

读取站点信息

  1. package com.example.extracttemp.demo.Utils;
  2. import java.io.*;
  3. import java.nio.Buffer;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /**
  7. * description
  8. *
  9. * @author ${user}
  10. * @Time 2019-06-06
  11. */
  12. public class StationUtils {
  13. private String filepath = "data/station.txt";
  14. public List<String> getStations() throws IOException {
  15. List<String> stationList = new ArrayList<>();
  16. File file = new File(filepath);
  17. FileReader fileReader = new FileReader(file);
  18. BufferedReader bufferedReader = new BufferedReader(fileReader);
  19. String tmpline = null;
  20. while ( (tmpline = bufferedReader.readLine()) != null ) {
  21. stationList.add(tmpline);
  22. }
  23. return stationList;
  24. }
  25. public static void main(String[] args) {
  26. StationUtils stationUtils = new StationUtils();
  27. try {
  28. List<String> stationlist = stationUtils.getStations();
  29. System.out.println(stationlist.size());
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

解析json字符串,并保存到文件中

  1. package com.example.extracttemp.demo.Service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.springframework.stereotype.Service;
  5. import java.io.*;
  6. import java.math.BigDecimal;
  7. import java.util.*;
  8. @Service
  9. public class ParseData {
  10. // Map<String, List<String>> YearAvgTemp = new HashMap<>();
  11. // Map<String, List<String>> YearAvgLandTemp =new HashMap<>();
  12. //
  13. // Map<String, List<String>> YearMinTemp = new HashMap<>();
  14. // Map<String, List<String>> YearMinLandTemp = new HashMap<>();
  15. //
  16. // Set<String> yearSet = new TreeSet<>();
  17. //
  18. // Map<String, Float> YearAvgTempResult = new HashMap<>();
  19. // Map<String, Float> YearAvgTempLandResult = new HashMap<>();
  20. //
  21. // Map<String, Integer> DayTempLess0Result = new HashMap<>();
  22. // Map<String, Integer> DayTempLess2Result = new HashMap<>();
  23. // Map<String, Integer> DayTempLess3Result = new HashMap<>();
  24. //
  25. // Map<String, Integer> LandTempLess0Result = new HashMap<>();
  26. StringBuffer sBuffer = new StringBuffer("");
  27. {
  28. // sBuffer.append("年份,");
  29. // sBuffer.append("年平均温度,");
  30. // sBuffer.append("年平均地表温度,");
  31. // sBuffer.append("日最低温度低于0℃以下日数,");
  32. // sBuffer.append("日最低温度低于2℃以下日数,");
  33. // sBuffer.append("日最低温度低于3℃以下日数,");
  34. // sBuffer.append("地面最低温度低于0℃以下日数");
  35. // sBuffer.append("\r\n");
  36. }
  37. StringBuffer sBuffer1 = new StringBuffer("");
  38. {
  39. sBuffer1.append("年份,");
  40. sBuffer1.append("站点,");
  41. sBuffer1.append("年平均温度,");
  42. sBuffer1.append("年平均地表温度,");
  43. sBuffer1.append("日最低温度低于0℃以下日数,");
  44. sBuffer1.append("日最低温度低于2℃以下日数,");
  45. sBuffer1.append("日最低温度低于3℃以下日数,");
  46. sBuffer1.append("地面最低温度低于0℃以下日数");
  47. sBuffer1.append("\r\n");
  48. }
  49. public void Parse(String path) {
  50. List<String> stationFileList = GetFiles(path);
  51. for(String filename: stationFileList) {
  52. Map<String, List<String>> YearAvgTemp = new HashMap<>();
  53. Map<String, List<String>> YearAvgLandTemp =new HashMap<>();
  54. Map<String, List<String>> YearMinTemp = new HashMap<>();
  55. Map<String, List<String>> YearMinLandTemp = new HashMap<>();
  56. Set<String> yearSet = new TreeSet<>();
  57. Map<String, Float> YearAvgTempResult = new HashMap<>();
  58. Map<String, Float> YearAvgTempLandResult = new HashMap<>();
  59. Map<String, Integer> DayTempLess0Result = new HashMap<>();
  60. Map<String, Integer> DayTempLess2Result = new HashMap<>();
  61. Map<String, Integer> DayTempLess3Result = new HashMap<>();
  62. Map<String, Integer> LandTempLess0Result = new HashMap<>();
  63. String content = getContent(filename);
  64. JSONObject jsonObject = JSONObject.parseObject(content);
  65. JSONArray jsonArray = jsonObject.getJSONArray("DS");
  66. String param = jsonObject.getString("requestParams");
  67. String stationId = GetStationId(param);
  68. if ( jsonArray.size() == 0 ) {
  69. return;
  70. }
  71. for ( int i = 0; i < jsonArray.size(); i++ ) {
  72. JSONObject jsonObject1 = (JSONObject)jsonArray.get(i);
  73. // GST_Avg 地面平均温度
  74. // GST_Min 地面最低温度
  75. // TEM_Avg 平均温度
  76. // TEM_Min 最低温度
  77. String sta = jsonObject1.getString("Datetime");
  78. String Gst_avg = jsonObject1.getString("GST_Avg");
  79. String Gst_Min = jsonObject1.getString("GST_Min");
  80. String Tem_Avg = jsonObject1.getString("TEM_Avg");
  81. String Tem_Min = jsonObject1.getString("TEM_Min");
  82. if ( "999999".equals(Gst_avg) ) {
  83. Gst_avg = "0.0";
  84. }
  85. if ( "999999".equals(Tem_Avg) ) {
  86. Tem_Avg = "0.0";
  87. }
  88. System.out.println(sta);
  89. String year = sta.split("-")[0];
  90. String month = sta.split("-")[1];
  91. yearSet.add(year);
  92. if ( (YearAvgTemp.keySet().contains(year)) && (year.compareTo("1960") > 0)) {
  93. YearAvgTemp.get(year).add(Tem_Avg);
  94. } else if (year.compareTo("1960") > 0) {
  95. List<String> tempList = new ArrayList<>();
  96. tempList.add(Tem_Avg);
  97. YearAvgTemp.put(year, tempList);
  98. }
  99. if ( ( YearAvgLandTemp.keySet().contains(year)) && (year.compareTo("1960") > 0)){
  100. YearAvgLandTemp.get(year).add(Gst_avg);
  101. } else if (year.compareTo("1960") > 0) {
  102. List<String> tempList1 = new ArrayList<>();
  103. tempList1.add(Gst_avg);
  104. YearAvgLandTemp.put(year, tempList1);
  105. }
  106. if( month.compareTo("07") >= 0) {
  107. // 下一年
  108. String tmpyear = Integer.valueOf(year) + 1 + "";
  109. if ( YearMinTemp.keySet().contains(tmpyear) ) {
  110. YearMinTemp.get(tmpyear).add(Tem_Min);
  111. } else {
  112. List<String> tmpList = new ArrayList<>();
  113. tmpList.add(Tem_Min);
  114. YearMinTemp.put(tmpyear, tmpList);
  115. }
  116. if ( YearMinLandTemp.keySet().contains(tmpyear)) {
  117. YearMinLandTemp.get(tmpyear).add(Gst_Min);
  118. } else {
  119. List<String> tmpList = new ArrayList<>();
  120. tmpList.add(Gst_Min);
  121. YearMinLandTemp.put(tmpyear, tmpList);
  122. }
  123. } else {
  124. // 今天
  125. if ( YearMinTemp.keySet().contains(year) ) {
  126. YearMinTemp.get(year).add(Tem_Min);
  127. } else {
  128. List<String> tmpList = new ArrayList<>();
  129. tmpList.add(Tem_Min);
  130. YearMinTemp.put(year, tmpList);
  131. }
  132. if ( YearMinLandTemp.keySet().contains(year)) {
  133. YearMinLandTemp.get(year).add(Gst_Min);
  134. } else {
  135. List<String> tmpList = new ArrayList<>();
  136. tmpList.add(Gst_Min);
  137. YearMinLandTemp.put(year, tmpList);
  138. }
  139. }
  140. }
  141. // 求年平均温度
  142. for( String tempyear: YearAvgTemp.keySet() ) {
  143. float tempsum = 0.0f;
  144. int count = 0;
  145. for(String tempvalue: YearAvgTemp.get(tempyear) ) {
  146. tempsum += Float.valueOf(tempvalue);
  147. count++;
  148. }
  149. float avgtemp = tempsum / count;
  150. BigDecimal bigDecimal = new BigDecimal((double)avgtemp);
  151. bigDecimal = bigDecimal.setScale(1, 4);
  152. avgtemp = bigDecimal.floatValue();
  153. YearAvgTempResult.put(tempyear, avgtemp);
  154. }
  155. // 求年平均地面温度
  156. for( String tempyear: YearAvgLandTemp.keySet() ) {
  157. float tempsum = 0.0f;
  158. int count = 0;
  159. for(String tempvalue: YearAvgLandTemp.get(tempyear) ) {
  160. tempsum += Float.valueOf(tempvalue);
  161. count++;
  162. }
  163. float avglandtemp = tempsum / count;
  164. BigDecimal bigDecimal = new BigDecimal((double)avglandtemp);
  165. bigDecimal = bigDecimal.setScale(1, 4);
  166. avglandtemp = bigDecimal.floatValue();
  167. YearAvgTempLandResult.put(tempyear, avglandtemp);
  168. }
  169. // 日最低温度天数
  170. for( String tempyear: YearMinTemp.keySet() ) {
  171. int less0count = 0;
  172. int less2count = 0;
  173. int less3count = 0;
  174. // 0
  175. for ( String tempvalue: YearMinTemp.get(tempyear) ) {
  176. float tempmin = Float.valueOf(tempvalue);
  177. if ( tempmin < 0.0 ) {
  178. less0count++;
  179. }
  180. if ( tempmin < 2.0 ) {
  181. less2count++;
  182. }
  183. if ( tempmin < 3.0 ) {
  184. less3count++;
  185. }
  186. }
  187. DayTempLess0Result.put(tempyear, less0count);
  188. DayTempLess2Result.put(tempyear, less2count);
  189. DayTempLess3Result.put(tempyear, less3count);
  190. }
  191. // 地面最低温度天数
  192. for ( String tempyear: YearMinLandTemp.keySet()) {
  193. int less0count = 0;
  194. for( String tempvalue: YearMinLandTemp.get(tempyear) ) {
  195. float tmpvalue = Float.valueOf(tempvalue);
  196. if ( tmpvalue <= 0.0 ) {
  197. less0count++;
  198. }
  199. }
  200. LandTempLess0Result.put(tempyear, less0count);
  201. }
  202. FileWriter fileWriter = null;
  203. FileWriter fileWriter2 = null;
  204. try {
  205. fileWriter = new FileWriter("E:\\StationResult\\result\\"+stationId+".csv", true);
  206. fileWriter2 = new FileWriter("E:\\StationResult\\result1\\allstations.csv", true);
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. sBuffer.append("年份,");
  211. sBuffer.append("年平均温度,");
  212. sBuffer.append("年平均地表温度,");
  213. sBuffer.append("日最低温度低于0℃以下日数,");
  214. sBuffer.append("日最低温度低于2℃以下日数,");
  215. sBuffer.append("日最低温度低于3℃以下日数,");
  216. sBuffer.append("地面最低温度低于0℃以下日数");
  217. sBuffer.append("\r\n");
  218. for( String year: yearSet) {
  219. if ( year.compareTo("1960") == 0 ) {
  220. continue;
  221. }
  222. sBuffer.append(year);
  223. sBuffer.append(",");
  224. sBuffer.append(YearAvgTempResult.get(year));
  225. sBuffer.append(",");
  226. sBuffer.append(YearAvgTempLandResult.get(year));
  227. sBuffer.append(",");
  228. sBuffer.append(DayTempLess0Result.get(year));
  229. sBuffer.append(",");
  230. sBuffer.append(DayTempLess2Result.get(year));
  231. sBuffer.append(",");
  232. sBuffer.append(DayTempLess3Result.get(year));
  233. sBuffer.append(",");
  234. sBuffer.append(LandTempLess0Result.get(year));
  235. sBuffer.append("\r\n");
  236. sBuffer1.append(year);
  237. sBuffer1.append(",");
  238. sBuffer1.append(stationId);
  239. sBuffer1.append(",");
  240. sBuffer1.append(YearAvgTempResult.get(year));
  241. sBuffer1.append(",");
  242. sBuffer1.append(YearAvgTempLandResult.get(year));
  243. sBuffer1.append(",");
  244. sBuffer1.append(DayTempLess0Result.get(year));
  245. sBuffer1.append(",");
  246. sBuffer1.append(DayTempLess2Result.get(year));
  247. sBuffer1.append(",");
  248. sBuffer1.append(DayTempLess3Result.get(year));
  249. sBuffer1.append(",");
  250. sBuffer1.append(LandTempLess0Result.get(year));
  251. sBuffer1.append("\r\n");
  252. }
  253. System.out.println(sBuffer.toString());
  254. try {
  255. fileWriter.write(sBuffer.toString());
  256. fileWriter2.write(sBuffer1.toString());
  257. sBuffer.delete(0, sBuffer.length());
  258. sBuffer1.delete(0, sBuffer1.length());
  259. fileWriter2.close();
  260. fileWriter.close();
  261. } catch (IOException e) {
  262. e.printStackTrace();
  263. }
  264. }
  265. }
  266. public String GetStationId(String content) {
  267. int index = content.indexOf("staIds");
  268. int index1 = content.indexOf(",", index);
  269. String stationid = content.substring(index + "staIds".length()+1, index1);
  270. return stationid;
  271. }
  272. public String getContent(String filepath) {
  273. BufferedReader reader = null;
  274. String tempString = null;
  275. try {
  276. reader = new BufferedReader(new FileReader(filepath));
  277. tempString = reader.readLine();
  278. reader.close();
  279. } catch (IOException e) {
  280. e.printStackTrace();
  281. }
  282. return tempString;
  283. }
  284. private List<String> GetFiles(String path) {
  285. List<String> tmpfilelist = new ArrayList<>();
  286. File tmpfilehandler = new File(path);
  287. for( File tmpfile: tmpfilehandler.listFiles()) {
  288. if ( tmpfile.isFile() ) {
  289. tmpfilelist.add(tmpfile.toString());
  290. }
  291. }
  292. return tmpfilelist;
  293. }
  294. }

从web pull数据

  1. package com.example.extracttemp.demo.Service;
  2. import com.example.extracttemp.demo.Utils.StationUtils;
  3. import org.springframework.stereotype.Service;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. @Service
  11. public class GetData {
  12. String prefix = "http://10.129.18.132:8080/api.do?interfaceId=getSurfEleByTimeRangeAndStaID&dataCode=SURF_CHN_MUL_DAY&timeRange=[19600701000000,20181231000000]&staIds=";
  13. String suffix = "&from=0&limitCnt=22000&eleValueRanges=&orderBy=Datetime:asc&elements=Datetime,TEM_Avg,TEM_Min,GST_Avg,GST_Min&userId=xxzx&pwd=f4a6e7dd9e3584c38e05cdff78e51e30&staLevels=&dataFormat=json";
  14. static List<String> stationList = null;
  15. static List<String> stationList1 = new ArrayList<>();
  16. static List<String> stationList2 = new ArrayList<>();
  17. FileWriter fileWriter = null;
  18. FileWriter fileWriter1 = null;
  19. static {
  20. try {
  21. stationList = new StationUtils().getStations();
  22. stationList1.addAll(stationList.subList(0,stationList.size()/2));
  23. stationList2.addAll(stationList.subList(stationList.size()/2, stationList.size()));
  24. // stationList1.addAll(stationList);
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public void GetData() {
  30. Thread thread = new Thread() {
  31. @Override
  32. public void run() {
  33. System.out.println("stationList1: " + stationList1.size());
  34. for (String stationid : stationList1) {
  35. String url = prefix + stationid + suffix;
  36. String result = GetUrlRequest(url);
  37. File file = new File("E:\\StationData\\" + stationid + ".json");
  38. try {
  39. fileWriter = new FileWriter(file);
  40. fileWriter.write(result);
  41. fileWriter.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. };
  48. Thread thread1 = new Thread() {
  49. @Override
  50. public void run() {
  51. System.out.println("statoinList2: " + stationList2.size());
  52. for (String stationid : stationList2) {
  53. String url = prefix + stationid + suffix;
  54. String result = GetUrlRequest(url);
  55. File file = new File("E:\\StationData\\" + stationid + ".json");
  56. try {
  57. fileWriter1 = new FileWriter(file);
  58. fileWriter1.write(result);
  59. fileWriter1.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. };
  66. thread.start();
  67. thread1.start();
  68. try {
  69. thread.join();
  70. thread1.join();
  71. } catch (InterruptedException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. public String GetUrlRequest(String url1) {
  76. HttpURLConnection connection = null;
  77. InputStream inputStream = null;
  78. BufferedReader br = null;
  79. String result = null;
  80. try {
  81. URL url = new URL(url1);
  82. connection = (HttpURLConnection) url.openConnection();
  83. connection.setRequestMethod("GET");
  84. connection.setConnectTimeout(15000);
  85. connection.setReadTimeout(60000);
  86. connection.connect();
  87. if (connection.getResponseCode() == 200) {
  88. inputStream = connection.getInputStream();
  89. br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  90. StringBuilder sbf = new StringBuilder();
  91. String temp = null;
  92. while ((temp = br.readLine()) != null) {
  93. sbf.append(temp);
  94. sbf.append("\r\n");
  95. }
  96. result = sbf.toString();
  97. System.out.println(result);
  98. }
  99. } catch (MalformedURLException e) {
  100. e.printStackTrace();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. } finally {
  104. if (null != br) {
  105. try {
  106. br.close();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. if (null != inputStream) {
  112. try {
  113. inputStream.close();
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. }
  119. return result;
  120. }
  121. }

13.读写文件

读写文本文件

  1. File file = new File("data\\50968.json");
  2. BufferedReader reader = null;
  3. try {
  4. System.out.println("以行为单位读取文件内容,一次读一整行:");
  5. reader = new BufferedReader(new FileReader(file));
  6. String tempString = null;
  7. tempString = reader.readLine();
  8. while ((tempString = reader.readLine()) != null) {
  9. // 显示行号
  10. System.out.println("line " + line + ": " + tempString.length());
  11. }
  12. reader.close();

14.Java通过HttpClient请求url

  1. public class HttpClient {
  2. public static void main(String[] args) {
  3. HttpURLConnection connection = null;
  4. InputStream inputStream = null;
  5. BufferedReader br = null;
  6. String result = null;
  7. try {
  8. URL url = new URL("https://www.baidu.com/");
  9. connection = (HttpURLConnection)url.openConnection();
  10. connection.setRequestMethod("GET");
  11. connection.setConnectTimeout(15000);
  12. connection.setReadTimeout(60000);
  13. connection.connect();
  14. if ( connection.getResponseCode() == 200 ) {
  15. inputStream = connection.getInputStream();
  16. br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  17. StringBuilder sbf = new StringBuilder();
  18. String temp= null;
  19. while( ( temp = br.readLine()) != null ) {
  20. sbf.append(temp);
  21. sbf.append("\r\n");
  22. }
  23. result = sbf.toString();
  24. System.out.println(result);
  25. }
  26. } catch (MalformedURLException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }finally {
  31. if (null != br ) {
  32. try {
  33. br.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. if ( null != inputStream ) {
  39. try {
  40. inputStream.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }
  47. }

pasrse

pom.xml

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.47</version>
  5. </dependency>
  1. {"requestTime":"20190423134131","returnCode":"1","takeTime":0.111,"responseTime":"20190423134131","response":2805,"hbaseTimeUsed":111,"requestParams":"{adminCodes=340000, times=20190423014000, limitCnt=1, dataFormat=json, elements=Datetime,Station_Id_C,PRE,PRE_1h, outDataCode=SURF_CHN_MUL_MIN, interfaceId=getSurfEleByTimeAndRegionForMM, pwd=f4a6e7dd9e3584c38e05cdff78e51e30, dataCode=QX_MM_ORIGIN, userId=xxzx}","DS":[{"PRE":0.0,"PRE_1h":0.1,"Station_Id_C":"I4157","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I4113","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"Station_Id_C":"I3806","PRE_1h":"999999","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I1509","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I1251","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I1604","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I1651","Datetime":"2019-04-23 01:40:00"},{"PRE":0.0,"PRE_1h":0.0,"Station_Id_C":"I5509","Datetime":"2019-04-23 01:40:00"}]}

parse json

  1. public class parseJson {
  2. public static void main(String[] args) throws IOException {
  3. String data = "{\"requestTime\":\"20190423134131\",\"returnCode\":\"1\",\"takeTime\":0.111," +
  4. "\"responseTime\":\"20190423134131\",\"response\":2805,\"hbaseTimeUsed\":111," +
  5. "\"requestParams\":\"{adminCodes=340000, times=20190423014000, limitCnt=1, dataFormat=json, " +
  6. "elements=Datetime,Station_Id_C,PRE,PRE_1h, outDataCode=SURF_CHN_MUL_MIN, " +
  7. "interfaceId=getSurfEleByTimeAndRegionForMM, pwd=f4a6e7dd9e3584c38e05cdff78e51e30, " +
  8. "dataCode=QX_MM_ORIGIN, userId=xxzx}\",\"DS\":[" +
  9. "{\"PRE\":0.0,\"PRE_1h\":0.1," + "\"Station_Id_C\":\"I4157\",\"Datetime\":\"2019-04-23 01:40:00\"}," +
  10. "{\"PRE\":0.0,\"PRE_1h\":0.0," + "\"Station_Id_C\":\"I4113\",\"Datetime\":\"2019-04-23 01:40:00\"}]}";
  11. JSONObject jsonObject = JSONObject.parseObject(data);
  12. JSONArray jsonArray = jsonObject.getJSONArray("DS");
  13. StringBuffer sBuffer = new StringBuffer("");
  14. File file = new File("data.txt");
  15. if ( !file.exists() ) {
  16. file.createNewFile();
  17. }
  18. FileWriter fileWriter = new FileWriter(file.getName(), true);
  19. for ( int i = 0; i < jsonArray.size(); i++){
  20. System.out.println(jsonArray.get(i));
  21. JSONObject jsonObject1 = (JSONObject)jsonArray.get(i);
  22. sBuffer.append(jsonObject1.getString("Datetime"));
  23. sBuffer.append(",");
  24. sBuffer.append(jsonObject1.getString("Station_Id_C"));
  25. sBuffer.append(",");
  26. sBuffer.append(jsonObject1.getString("PRE"));
  27. sBuffer.append(",");
  28. sBuffer.append(jsonObject1.getString("PRE_1h"));
  29. sBuffer.append("\r\n");
  30. // System.out.println(jsonObject1.getString("PRE"));
  31. // System.out.println(jsonObject1.getString("PRE_1h"));
  32. // System.out.println(jsonObject1.getString("Station_Id_C"));
  33. // System.out.println(jsonObject1.getString("Datetime"));
  34. }
  35. System.out.println(sBuffer.toString());
  36. fileWriter.write(sBuffer.toString());
  37. fileWriter.close();
  38. }
  39. }

解析json

  1. package com.example.demo.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Service;
  6. import java.io.*;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. @Service
  15. public class ExtractService {
  16. @Value("${date}")
  17. private String date;
  18. HttpURLConnection connection = null;
  19. InputStream inputStream = null;
  20. BufferedReader br = null;
  21. String result = null;
  22. FileWriter fileWriter = null;
  23. public void start() {
  24. String[] tmpdate = date.split(",");
  25. System.out.println("tmpdate:"+ tmpdate.length);
  26. for (String d : tmpdate) {
  27. try {
  28. File file = new File("mmData/" + d + ".csv");
  29. if ( !file.exists() ) {
  30. file.createNewFile();
  31. }
  32. fileWriter = new FileWriter(file.getName(), true);
  33. System.out.println("date: " + d);
  34. extractDataByMM(d);
  35. fileWriter.close();
  36. try {
  37. Thread.sleep(2000);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. } catch (ParseException e) {
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. private void extractDataByMM(String date) throws ParseException {
  49. // parse date 20190423014000
  50. // 20190305000000
  51. // 20190423014000
  52. // System.out.println(date+"000000");
  53. date = date + "000000";
  54. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  55. Date parseData = simpleDateFormat.parse(date);
  56. Calendar calendar = Calendar.getInstance();
  57. calendar.setTime(parseData);
  58. calendar.add(Calendar.DAY_OF_MONTH, 1);
  59. String endTime = simpleDateFormat.format(calendar.getTime());
  60. calendar.setTime(parseData);
  61. // calendar.add(Calendar.MINUTE, 1);
  62. String startTime = simpleDateFormat.format(calendar.getTime());
  63. // System.out.println(simpleDateFormat.format(calendar.getTime()));
  64. while (true) {
  65. if (startTime.equals(endTime))
  66. break;
  67. getUrl(startTime);
  68. calendar.add(Calendar.MINUTE, 1);
  69. startTime = simpleDateFormat.format(calendar.getTime());
  70. }
  71. return;
  72. }
  73. private void getUrl(String date) {
  74. String url = "http://10.129.18.132:8080/api.do?interfaceId=getSurfEleByTimeAndRegionForMM&dataCode=SURF_CHN_MUL_MIN&times=" +
  75. date +
  76. "&adminCodes=340000&elements=Datetime,Station_Id_C,PRE,PRE_1h&limitCnt=1&userId=xxzx&pwd=f4a6e7dd9e3584c38e05cdff78e51e30&dataFormat=json";
  77. //
  78. // String url = "http://10.129.18.132:8080/api.do?interfaceId=getSurfEleByTimeAndRegionForMM&dataCode=SURF_CHN_MUL_MIN&times=20190423014000&adminCodes=340000&elements=Datetime,Station_Id_C,PRE,PRE_1h&limitCnt=1&userId=xxzx&pwd=f4a6e7dd9e3584c38e05cdff78e51e30&dataFormat=json";
  79. String result = getRequest(url);
  80. ParseResutl(result);
  81. }
  82. private String getRequest(String url1) {
  83. try {
  84. URL url = new URL(url1);
  85. connection = (HttpURLConnection) url.openConnection();
  86. connection.setRequestMethod("GET");
  87. connection.setConnectTimeout(15000);
  88. connection.setReadTimeout(60000);
  89. connection.connect();
  90. if (connection.getResponseCode() == 200) {
  91. inputStream = connection.getInputStream();
  92. br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  93. StringBuilder sbf = new StringBuilder();
  94. String temp = null;
  95. while ((temp = br.readLine()) != null) {
  96. sbf.append(temp);
  97. sbf.append("\r\n");
  98. }
  99. result = sbf.toString();
  100. // System.out.println(result);
  101. }
  102. } catch (MalformedURLException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. } finally {
  107. if (null != br) {
  108. try {
  109. br.close();
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. if (null != inputStream) {
  115. try {
  116. inputStream.close();
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }
  122. return result;
  123. }
  124. private void ParseResutl(String data) {
  125. JSONObject jsonObject = JSONObject.parseObject(data);
  126. JSONArray jsonArray = jsonObject.getJSONArray("DS");
  127. if ( jsonArray.size() == 0 ) {
  128. return;
  129. }
  130. StringBuffer stringBuffer = new StringBuffer("");
  131. for ( int i = 0; i < jsonArray.size(); i++){
  132. // System.out.println(jsonArray.get(i));
  133. JSONObject jsonObject1 = (JSONObject)jsonArray.get(i);
  134. stringBuffer.append(jsonObject1.getString("Datetime"));
  135. stringBuffer.append(",");
  136. stringBuffer.append(jsonObject1.getString("Station_Id_C"));
  137. stringBuffer.append(",");
  138. stringBuffer.append(jsonObject1.getString("PRE"));
  139. stringBuffer.append(",");
  140. stringBuffer.append(jsonObject1.getString("PRE_1h"));
  141. stringBuffer.append("\r\n");
  142. }
  143. System.out.println("result:" + stringBuffer.toString());
  144. try {
  145. fileWriter.write(stringBuffer.toString());
  146. fileWriter.flush();
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. }