Java 字符串(时间)

String 是只读对象。如果需要对字符串做修改,应该使用 StringBuffer & StringBuilder 类。

String 直接创建的字符串存储在公共池?中,而 new 创建的字符串对象在堆?上。

		String s1 = "TEST";               // String 直接创建
		String s2 = s1;                   // 相同引用,指向一个对象而不是创建一个对象
		String s3 = new String("TEST");   // String 对象创建,构造函数创建

通过字符数组创建字符串

		char[] helloArray = { 'T','E','S','T'};
	    String helloString = new String(helloArray);

字符串的比较

		String a = "Hello World";//存到了字符串池的某个地址上
		String b = "Hello World";//字符串池中有了,直接复用这个地址
		String c = new String("Hello World");//拿一个新的地址空间来存储
		
		System.out.println(a==b);            //地址一致
		System.out.println(a.equals(b));     //内容一致
		
		System.out.println(a==c);			 //地址不一致
		System.out.println(a.equals(c));     //内容一致
		/*比较地址意义不大,所以几乎不用。一般用equals来比较内容上是否一致。*/

String 访问器 方法(获取有关对象的信息的方法称为访问器方法)

        字符串长度

 int len = helloString.length();
 System.out.println( "helloString长度 : " + len );

        .concat 连接字符串 

		String coc= new String("haha");
		String con= new String("abc"+"ded"+coc);//直接连接

        字符位置

		String strings="abcdefgcda";
		System.out.println(strings.charAt(0));//查询某位置的字符
		System.out.println(strings.indexOf('b'));//查询某字符的位置
		System.out.println(strings.indexOf("cda"));//查询某字符串的首字符位置

        .format 格式化字符串

        格式化有关内容的符号如下图所示:

%s字符串%x十六进制整%c指数形式%n换行符
%c字符%o八进制整%g通用浮点%t?日期时间
%b逻辑%f十进制浮点%h散列码形式
%d十进制整%a十六进浮%%百分比形式
float a=1.0F;
int b=2;
String c="hello";

System.out.printf("浮点型变量的值为 " +
                  "%f, 整型变量的值为 " +
                  " %d, 字符串变量的值为 " +
                  "is %s",a,b,c);

String fs;
fs = String.format("浮点型变量的值为 " +
                   "%f, 整型变量的值为 " +
                   " %d, 字符串变量的值为 " +
                   " %s",a,b,c);
System.out.println(fs);

打印格式化日期时间

tF年-月-日tr时:分:秒  上午(12H)
tD月/日/年tT时分秒 (24H)
tR时:分(24H)
		Date today=new Date();
		String a = String.format("%tF", today);
		System.out.println(a);

 DateFormat格式化日期时间

		Date date = new Date();						//获取系统时间
		DateFormat df = null;						//
		df = DateFormat.getDateInstance();			//
		System.out.println("日期:"+df.format(date)); //日期:2021年9月9日

 SimpleDateFormat格式化日期时间

符号描述符号描述
a上/下午D某天(1-366)
d某天(1-31)E星期X
h某时(12)F某月工作日数
k某时(24)G纪元(AD/BC)
m某分(1-59)H某小时(1-23)
s某秒(1-59)K上午或下午某时(1-11)
w某星期(1-52)M月份
yS秒钟之毫秒
z时区W月之星期数(1-5)
		Date dates = new Date();
		SimpleDateFormat sdf = null;
		sdf = new SimpleDateFormat("yyyy/MMM/dd hh:mm:ss a");
		System.out.println(sdf.format(dates));
		sdf = new SimpleDateFormat("yyyy年MM月dd日");
		System.out.println(sdf.format(dates));
		sdf = new SimpleDateFormat("E yy-MM-dd h:mm:ss z");
		System.out.println(sdf.format(dates));

		/*
		2021/9月/09 06:59:17 下午
		2021年09月09日
		周四 21-09-09 6:59:17 CST
		*/

NumberFormat数字格式化

		NumberFormat nf = null;
		nf = NumberFormat.getNumberInstance();
		System.out.println(nf.format(123456.789));//一般
		
		nf = NumberFormat.getNumberInstance(Locale.FRENCH);
		System.out.println(nf.format(123456.789));//法国数字表示
		
		nf = NumberFormat.getCurrencyInstance();
		System.out.println(nf.format(123456.789));//货币显示
		
		nf = NumberFormat.getPercentInstance();
		System.out.println(nf.format(0.25));//百分数显示

更多string方法: Java String 类 | 菜鸟教程

String Buffer 字符串缓冲器

		StringBuffer test=new StringBuffer("");  //实例化字符串缓存对象
		test.append('a'+",");                    //使用添加方法
		test.append('b');                        //使用添加方法                  
		System.out.println(test);                //打印测试
        System.out.println(test.toString());     //转化为字符串打印测试

注意:在append连接多个字符时,应至少使用一对双引号,如果仅使用单引号将作为数字相加。

注意:toString方法并不会改变他的buffer性质,只是对本次调用转换,一般调用不必转为字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,字符串时间转换是一种常见的操作。有时我们需要将字符串转换为Java中的Date类型,有时我们需要将Date类型转换为字符串。在这个过程中,有一些细节需要注意。下面我们来详细解析一下Java字符串时间转换。 1.字符串转Date类型 在将字符串转换为Date类型时,我们需要使用SimpleDateFormat类。这个类可以根据我们的需求定义日期和时间的格式,并将字符串转换为Date类型。下面是一个示例代码: public static Date stringToDate(String dateString) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(dateString); } catch (ParseException e) { throw e; } return date; } 在这个方法中,我们将字符串类型的日期转换成了Java中的Date类型。在定义SimpleDateFormat对象时,我们需要指定日期格式,以便正确地解析字符串。例如,上面的代码中指定的日期格式为“yyyy-MM-dd HH:mm:ss”,就代表日期格式为“年-月-日 时:分:秒”。 2.Date类型转字符串Java中的Date类型转换为字符串同样需要使用SimpleDateFormat类。我们可以在创建SimpleDateFormat对象时指定日期格式,然后调用format()方法将Date类型转换为字符串。下面是一个示例代码: public static String dateToString(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = format.format(date); return dateString; } 在这个方法中,我们将Date类型的日期转换成了字符串类型。格式和前面一样,只需要调用SimpleDateFormat类的format()方法将Date类型转换为字符串。这个方法会返回转换后的字符串。 总结 字符串时间转换是Java中常见的操作之一。我们需要使用SimpleDateFormat类来完成字符串和Date类型之间的转换。在使用SimpleDateFormat类时,我们需要定义日期格式以及调用相应的方法,才能正确地转换字符串和日期。在实际开发中,我们需要注意格式的正确性,并处理可能出现的异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的海螺呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值