• Java基本类型转换总结



    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。http://orajc.blog.51cto.com/458434/94622

    数值型转换成字符型  // 基本数据类型,
      int i_a =7;
      String str_a;
      str_a = String.valueOf(i_a);
      System.out.println(str_a);
      str_a = String.format("%06d", i_a);
      System.out.println(str_a);
      //
    封装类型
      Integer intr = new Integer("123");
      str_a=intr.toString();
      System.out.println(str_a);
    ―――――――――――――――――――――
    总结
    String.valueOf(),Stirng.format(),Object.toStirng();
    ――――――――――――――――――――――――――――――
    字符型转换成数值型
      //字符型转换成数值型
      String str_b ="111";
      int i_b =0;
      i_b = Integer.parseInt(str_b);
      System.out.println("i_b = " + i_b);
    ――――――――――――――――――――――――――――――――――――
    字符型转换成日期型
    Java语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分. 日期是商业逻辑计算一个关键的部分. 所有的开发者都应该能够计算未来的日期, 定制日期的显示格式, 并将文本数据解析成日期对象.

    1、具体类(和抽象类相对)java.util.Date 
    2、抽象类java.text.DateFormat 和它的一个具体子类,java.text.SimpleDateFormat 
    3、抽象类java.util.Calendar 和它的一个具体子类,java.util.GregorianCalendar

    Date 类实际上只是一个包裹类, 它包含的是一个长整型数据,表示的是从GMT(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数.
    longnow = System.currentTimeMillis();
      String str_c = String.format("%tR", now); // "16:04"
      System.out.println("str_c1 = " + str_c);
      Date d = new Date(now);
      str_c = String.format("%tD", d); // "06/17/08"
      System.out.println("str_c2 = " + str_c);
      str_c = String.valueOf(d); // Tue Jun 17 16:13:08 CST 2008
      System.out.println("str_c3 = " + str_c);
      Date date = new Date();
      System.out.println(date.getTime());// 1213691167226

    用Calendar 类设置和获取日期数据的特定部分呢, 比如说小时, 日, 或者分钟,在日期的这些部分加上或者减去值
    ――――――――――――――――――――――――――――――――――――
    日期型转换成字符型

    DateFormatf = new SimpleDateFormat("yyyy-MM-dd");
      try {
      Date d_a = f.parse("2005-11-07");
      System.out.println("d_a = " + d_a);// Mon Aug 07 23:00:00 CST2006
      } catch (ParseException e) {
      e.printStackTrace();
      }
      SimpleDateFormat bartDateFormat = new SimpleDateFormat(
      "EEEE-MMMM-dd-yyyy");
      Date d_2 = new Date();
      System.out.println("d_2 = " + bartDateFormat.format(d_2));//
    星期二-六月-17-2008
      SimpleDateFormat bartDateFormat2 = newSimpleDateFormat("MM-dd-yyyy");
      // Create a string containing a text date to be parsed.
      String dateStringToParse = "9-29-2001";
      try {
      // Parse the text version of the date.
      // We have to perform the parse method in a
      // try-catch construct in case dateStringToParse
      // does not contain a date in the format we are expecting.
      Date date3 = bartDateFormat2.parse(dateStringToParse);
      // Now send the parsed date as a long value
      // to the system output.
      System.out.println(date3.getTime());
      } catch (Exception ex) {
      System.out.println(ex.getMessage());
      }
      System.out.println("--------------------------------------------");
      // -----------------------------
      DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
      // Create our Gregorian Calendar.
      GregorianCalendar cal = new GregorianCalendar();
      // Set the date and time of our calendar
      // to the system&s date and time
      cal.setTime(new Date());
      System.out.println("System Date: " +dateFormat.format(cal.getTime()));
      // Set the day of week to FRIDAY
      cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);
      System.out.println("After Setting Day of Week to Friday: "
      + dateFormat.format(cal.getTime()));
      int friday13Counter = 0;
      while (friday13Counter <= 10) {
      // Go to the next Friday by adding 7 days.
      cal.add(GregorianCalendar.DAY_OF_MONTH, 7);
      // If the day of month is 13 we have
      // another Friday the 13th.
      if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) {
      friday13Counter++;
      System.out.println(dateFormat.format(cal.getTime()));
      }
      }
    ――――――――――――――――――――――――――――――――――――

    本文出自 “achilles” 博客,请务必保留此出处http://orajc.blog.51cto.com/458434/94622

  • 相关阅读:
    C语言开发CGI程序的简单例子
    js收集错误信息,错误上报
    php安装pear、pecl
    (转)进程与线程的一个简单解释
    php curl 中的gzip压缩性能测试
    (转载):() { :|:& }; : # <-- 打开终端,输入这个,回车.你看到了什么??
    (转)open和fopen的区别:
    C语言中typedef
    secureCRT使用VIM 像LINUX中那样对语法高亮
    iframe与主框架跨域相互访问方法
  • 原文地址:https://www.cnblogs.com/liaoshiyong/p/3150935.html
Copyright © 2020-2023  润新知