zoukankan      html  css  js  c++  java
  • java字符串类型和时间类型的转换

    
    
    类型转换
    
    
    
     //reqeust.getParameter获取字符串直接赋值
    1
    public static Date date(String date_str) { 2 try { 3 Calendar zcal = Calendar.getInstance();//日期类 4 Timestamp timestampnow = new Timestamp(zcal.getTimeInMillis());//转换成正常的日期格式 5 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改为需要的东西 6 ParsePosition pos = new ParsePosition(0); 7 java.util.Date current = formatter.parse(date_str, pos); 8 timestampnow = new Timestamp(current.getTime()); 9 return timestampnow; 10 } 11 catch (NullPointerException e) { 12 return null; 13 } 14 }
     1 //Date类型转换成String类型
     2  public static String toJson(Object obj){
     3        String reuqest=null;
     4         //对象映射
     5          ObjectMapper mapper=new ObjectMapper();
     6           //设置时间格式
     7          SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日");
     8           mapper.setDateFormat(dateFormat);
     9              try {
    10                  reuqest=mapper.writeValueAsString(obj);
    11              } catch (JsonProcessingException e) {
    12                  // TODO Auto-generated catch block
    13                 e.printStackTrace();
    14             }
    15         return reuqest;
    16      }
     
    //String类型转换成Date类型

    1 public static Date date(String date_str) {
     2         try {
     3             Calendar zcal = Calendar.getInstance();//日期类
     4             Timestamp timestampnow = new Timestamp(zcal.getTimeInMillis());//转换成正常的日期格式
     5             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改为需要的东西
     6             ParsePosition pos = new ParsePosition(0);
     7             java.util.Date current = formatter.parse(date_str, pos);
     8             timestampnow = new Timestamp(current.getTime());
     9             return timestampnow;
    10         }
    11         catch (NullPointerException e) {
    12             return null;
    13         }
    14     }


  • 相关阅读:
    api1
    录像时调用MediaRecorder的start()时发生start failed: -19错误
    继承AppCompatActivity的Activity隐藏标题栏
    Android 6.0 运行时权限处理完全解析
    Android开发用过的十大框架
    Lite Your Android English
    2015最流行的Android组件、工具、框架大全
    C#调用C++函数入口点的问题 z
    C#调用C++的DLL函数另一则(delegate) z
    C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9173245.html
Copyright © 2011-2022 走看看