//Int型数字转换成String
int num1=123456;
//方法1
String str1=num1+"";
System.out.println(str1);
//方法2
String str2=String.valueOf(num1);
System.out.println(str2);

//String转换成Int型数字
String str3=new String("9876543");
int num2=Integer.parseInt(str3);
System.out.println(num1+num2);

//字符转换成字符型数组
String str4="HRuinger";
char a[]=str4.toCharArray();
for(int i:a)
System.out.print((char)i+" ");
System.out.println();

//字符型数组转换成字符串
char b[]={'H','R','u','i','n','g'};
String str5=String.valueOf(b);
System.out.println(str5);

//整型与字符型数值的转换
char c='3';
System.out.println(c-'0'); //此处也可以为c-48
int d=5;
System.out.println((char)(d+'0')); //此处也可以为c+48

如何将long类型转换为string
两种方法:
一、使用String类的方法,String.valueOf(),比如:
long aa = 123;
String a = String.valueOf(aa);
二、最简单的直接将long类型的数据加上一个空串
long aa = 123;
String a = aa+"";
Integer转String
//方法一:
Integer类的静态方法
toString()Integer a = 2;
String str = Integer.toString(a)

//方法二:Integer类的成员方法
toString()Integer a = 2;
String str = a.toString();

//方法三:String类的静态方法
valueOf()Integer a = 2;
String str = String.valueOf(a);
二、String转Integer
当我们要把String转化为Integer时,一定要对String进行非空判断,否则很可能报空指针异常。
String str = "...";

Integer i = null;

if(str!=null){

i = Integer.valueOf(str);

}

获取一个变量的类型

public static void main(String[] args) {

Test test = new Test();
        System.out.println(test.getClass().getSimpleName());
    }
}

1.Integer转换成int的方法

Integer i;  int k = i.intValue(); 即Integer.intValue();

2.int转换成Integer

int i;

Integer it = new Integer(i);

3.String转换成int的方法

String str = "10";   Integer it = new Interger(str);

int i = it.intValue();

即:int i = Integer.intValue(string);

4.int转换成String

int i;

(1)String s = String.valueOf(i);

(2)String s = Ingeger.toString(i);

(3)String s = "" + i;

5.String转换成Integer

String str = "10"

Integer it = Integer.valueOf(str);

6.Integer转换成String

Integer it;

String str = it.toString();

7.String转换成BigDecimal

BigDecimal bd = new BigDecimal(str);

8.日期

Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DATE);

//获取今天的日期字符串 String today = Java.text.DateFormat.getDateInstance().format(new java.util.Date()); //获取今天的日期 new java.sql.Date(System.currentTimeMillis());

Java中的Date时间转换【SimpleDateFormat (parse和format)】和Calendar日历表

parse:将字符串转换成时间
format:将时间转换成字符串


 1 package object;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 public class Date1 {
8
9 public static void main(String[] args) throws ParseException {
10 // TODO Auto-generated method stub
11 Date date=new Date();
12 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
13 String time1=format.format(date);//将时间转换成字符串
14 System.out.println(time1);
15
16 String time2="1996-09-18 07:18:09";
17 SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
18 Date date2=format2.parse(time2);//将字符串转换成时间
19 System.out.println(date2);
20
21 }
22
23 }
24 //结果:
25 2017-12-20 15:49:49
26 Wed Sep 18 07:18:09 GMT+08:00 1996

 Date应用实例:

 1 package object;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 public class DateBirthday {
8
9 public static void main(String[] args) throws ParseException, InterruptedException {
10 // TODO Auto-generated method stub
11 //求两个人的生日相差多少天
12 String birthday1="1996-09-18";
13 String birthday2="1993-08-27";
14 SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
15 Date date1=format1.parse(birthday1);
16 Date date2=format1.parse(birthday2);
17 long l1=date1.getTime();
18 long l2=date2.getTime();
19 long l3=l2-l1;
20 int day=Math.abs((int) (l3/1000/60/60/24));
21 System.out.println("两人相差"+day+"天");//结果:两人相差1096天
22
23
24 //没个两秒显示一次时间
25 while(true){
26 new Thread().sleep(2000);
27 Date date=new Date();
28 SimpleDateFormat format3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
29 String time=format3.format(date);
30 System.out.println(time);
31 }
32 }
33
34 }

利用Calendar日历表分别获取时间,设置时间:


 1 package object;
2
3 import java.util.Calendar;
4 import java.util.Date;
5
6 public class Date2 {
7 public static void main(String[] args) {
8 Calendar c = Calendar.getInstance();
9 c.add(Calendar.YEAR, 3);//年+3年
10 c.set(2018,1,1);//重新设置时间
11 int year = c.get(Calendar.YEAR);
12 int month = c.get(Calendar.MONTH);//获取的月份是 0-11,所以实际月份要+1
13 int date = c.get(Calendar.DATE);
14 int hour = c.get(Calendar.HOUR_OF_DAY);
15 int minute = c.get(Calendar.MINUTE);
16 int second = c.get(Calendar.SECOND);
17
18 System.out.println(year);//年
19 System.out.println(month);//月份
20 System.out.println(hour);//小时
21 System.out.println(minute);//分钟
22 System.out.println(second);//秒
23 System.out.println(date);//日
24
25
26 }
27
28 }

---------------------
作者:NPException
来源:CSDN
原文:https://blog.csdn.net/qq_36850813/article/details/79769400
版权声明:本文为博主原创文章,转载请附上博文链接!

【转载】java的常见类型转换的更多相关文章

  1. Java基础常见英语词汇

    Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的                             ['prəʊɡræmɪŋ]编程 OO: object ...

  2. Java英文单词Java基础常见英语词汇

    Java英文单词Java基础常见英语词汇(共70个)                                                                          ...

  3. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

  4. Java总结——常见Java集合实现细节(1)

    Java提高——常见Java集合实现细节(1) 2018年04月18日 15:07:35 阅读数:25 集合关系图 Set和Map set代表一种集合元素无序.集合元素不可重复的集合 map代表一种由 ...

  5. java中常见的六种线程池详解

    之前我们介绍了线程池的四种拒绝策略,了解了线程池参数的含义,那么今天我们来聊聊Java 中常见的几种线程池,以及在jdk7 加入的 ForkJoin 新型线程池 首先我们列出Java 中的六种线程池如 ...

  6. Java 基础常见知识点&面试题总结(下),2022 最新版!

    你好,我是 Guide.秋招即将到来,我对 JavaGuide 的内容进行了重构完善,同步一下最新更新,希望能够帮助你. 前两篇: Java 基础常见知识点&面试题总结(上),2022 最新版 ...

  7. java中强制类型转换

    在Java中强制类型转换分为基本数据类型和引用数据类型两种,这里我们讨论的后者,也就是引用数据类型的强制类型转换. 在Java中由于继承和向上转型,子类可以非常自然地转换成父类,但是父类转换成子类则需 ...

  8. 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型

    问题描述: java里面时间类型转换成json数据就成这样了: "createTime":{"date":30,"day":3," ...

  9. Java Socket常见异常处理 和 网络编程需要注意的问题

    在java网络编程Socket通信中,通常会遇到以下异常情况: 第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发 ...

随机推荐

  1. 用于改善质量、稳定性和多样性的可增长式GAN

    用于改善质量.稳定性和多样性的可增长式GAN GANs NVIDIA Fly real or fake ? real or fake ? 1024 x 1024 images generated us ...

  2. jQuery EasyUI 1.4更新记录

    问题: menu:修复当删除一个menu项时.menu高度不准确. datagrid:修复当datagrid宽度太小时,fitColumns方法不能使用. 改进: 1.为easyui全部组件新增了自适 ...

  3. 【面试】【Spring常见问题总结】【07】

    [常见面试问题总结文件夹>>>] 61.Spring IoC容器的依赖有两层含义: Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean并管理Bean的 ...

  4. Codeforces Round #257 (Div. 2/B)/Codeforces450B_Jzzhu and Sequences

    B解题报告 算是规律题吧,,,x y z -x -y -z 注意的是假设数是小于0,要先对负数求模再加模再求模,不能直接加mod,可能还是负数 给我的戳代码跪了,,. #include <ios ...

  5. [Apple开发者帐户帮助]三、创建证书(2)创建开发者ID证书

    您可以使用开发人员帐户或Xcode 创建最多五个开发者ID应用程序证书和最多五个开发人员ID安装程序证书.(要在Xcode中创建开发者ID证书,请转到Xcode帮助中的管理签名证书.) 所需角色:帐户 ...

  6. Python 41 完整查询语句 和 一堆关键字

    一:完整查询语句 1.拷贝表 *** create table copy_table select *from customer ; 拷贝结构 与数据 create table copy_table ...

  7. BZOJ 3681 线段树合并+网络流

    思路: 暴力建图有n*m条边 考虑怎么优化 (那就只能加个线段树了呗) 然后我就不会写了..... 抄了一波题解 //By SiriusRen #include <bits/stdc++.h&g ...

  8. C - New Year Candles

    Problem description Vasily the Programmer loves romance, so this year he decided to illuminate his r ...

  9. collectionView必须点击两次才跳转

    今天遇到一个很奇怪的现象:collectionView必须点击两次才能跳转.具体看代码: -(void)collectionView:(UICollectionView *)collectionVie ...

  10. RFC1867 HTTP file upload

    RFC1867 HTTP file upload RFC1867 is the standard definition of that "Browse..." button tha ...