public class string {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
sbappend("I am");
sbappend("a student");sbappend("我是中国人");
String s=sbtoString();
Systemoutprintln("连接字符串为:"+s);
Systemoutprintln("第8个字符为:"+scharAt(8-1));
//由于第一个是从0开始,所以要减一,空格是一个字符
Systemoutprintln("第16个字符为:"+strim()charAt(16-1));
Systemoutprintln("中国得位置:"+sindexOf("中国"));
Systemoutprintln("大写为:"+stoUpperCase());
}
}
1java中关于获取时间的所有方式如下,
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = CalendargetInstance(); //得到日历
calendarsetTime(dNow);//把当前时间赋给日历
calendaradd(CalendarDAY_OF_MONTH, -1); //设置为前一天
dBefore = calendargetTime(); //得到前一天的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置时间格式
String defaultStartDate = sdfformat(dBefore); //格式化前一天
String defaultEndDate = sdfformat(dNow); //格式化当前时间
Systemoutprintln("前一天的时间是:" + defaultStartDate);
Systemoutprintln("生成的时间是:" + defaultEndDate);
2如上方式能得到当前时间的前一天时间,同理,可以得到前一天的前一天,那么,前几天的时间都可以这样获得,如果觉得方案还是不可行,可以使用第三方的日历组件来获得更加准确的全年三百六十五天的时间
另有:
Calendar 与 Date 的转换:
Calendar calendar = CalendargetInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendargetTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// 先获得一个实例,然后设置 Date 对象
calendarsetTime(date);
月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8
calendarset(CalendarMONTH, 7);
计算2000-01-01是星期几
calendarget(calendarDAY_OF_WEEK )
3实例代码测试如下
Java代码import Javautil;
public class ShowDate {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();
calendarsetTime(trialTime);
// print out a bunch of interesting things
Systemoutprintln("ERA: " + calendarget(CalendarERA));
Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));
Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));
Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));
Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));
Systemoutprintln("DATE: " + calendarget(CalendarDATE));
Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));
Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));
Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));
Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));
Systemoutprintln("AM_PM: " + calendarget(CalendarAM_PM));
Systemoutprintln("HOUR: " + calendarget(CalendarHOUR));
Systemoutprintln("HOUR_OF_DAY: " + calendarget(CalendarHOUR_OF_DAY));
Systemoutprintln("MINUTE: " + calendarget(CalendarMINUTE));
Systemoutprintln("SECOND: " + calendarget(CalendarSECOND));
Systemoutprintln("MILLISECOND: " + calendarget(CalendarMILLISECOND));
Systemoutprintln("ZONE_OFFSET: " + (calendarget(CalendarZONE_OFFSET)/(60601000)));
Systemoutprintln("DST_OFFSET: " + (calendarget(CalendarDST_OFFSET)/(60601000)));
Systemoutprintln("Current Time, with hour reset to 3");
calendarclear(CalendarHOUR_OF_DAY); // so doesn't override
calendarset(CalendarHOUR, 3);
Systemoutprintln("ERA: " + calendarget(CalendarERA));
Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));
Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));
Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));
Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));
Systemoutprintln("DATE: " + calendarget(CalendarDATE));
Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));
Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));
Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));
Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));
package test;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilDate;
import javautilLocale;
public class DateParser {
public static void main(String[] args) {
//java时间格式转换: Sep 29, 2012 1:00:01 AM 如何转换成标准的java Date对象
/
LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear1996; 96
MMonth in yearMonthJuly; Jul; 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesday; Tue
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
/
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a",LocaleENGLISH);
Date d2 = null;
try {
d2 = sdfparse("Sep 29, 2012 1:00:01 AM");
} catch (ParseException e) {
eprintStackTrace();
}
Date d3 = new Date();
Systemoutprintln("d2 ====== "+d2);
Systemoutprintln("d3 ====== "+sdfformat(d3));
}
}
calget(CalendarHOUR_OF_DAY) 取的就是24时钟数
calget(CalendarHOUR) 取的就是12时钟数
看一下它们的注释就清楚了。
/
Field number for <code>get</code> and <code>set</code> indicating the
hour of the morning or afternoon <code>HOUR</code> is used for the
12-hour clock (0 - 11) Noon and midnight are represented by 0, not by 12
Eg, at 10:04:15250 PM the <code>HOUR</code> is 10
@see #AM_PM
@see #HOUR_OF_DAY
/
public final static int HOUR = 10;
/
Field number for <code>get</code> and <code>set</code> indicating the
hour of the day <code>HOUR_OF_DAY</code> is used for the 24-hour clock
Eg, at 10:04:15250 PM the <code>HOUR_OF_DAY</code> is 22
@see #HOUR
/
public final static int HOUR_OF_DAY = 11;
如果使用的是calget(CalendarHOUR),可能通过calget(CalendarAM_PM)的返回值来判断是上午还是下午,如果返回0,则是上午,如果返回1,则是下午。判断的时候,最好用类定义的常量来比较。
if(CalendarAM==calget(CalendarAM_PM)){
//上午
}else /if(CalendarPM==calget(CalendarAM_PM))/{
//下午
}
java中标点字符包括非字母、数字的字符。\x0d\用正则表达式比较简单,思路:把所有的标点替换成",",然后分割",",统计分割的次数。\x0d\str = strreplaceAll("[\\pP‘’“”]", ",");\x0d\\x0d\\pP 其中的小写 p 是 property 的意思,表示 Unicode 属性,用于 Unicode 正表达式的前缀。\x0d\大写 P 表示 Unicode 字符集七个字符属性之一:标点字符。\x0d\其他六个是\x0d\L:字母;\x0d\M:标记符号(一般不会单独出现);\x0d\Z:分隔符(比如空格、换行等);\x0d\S:符号(比如数学符号、货币符号等);\x0d\N:数字(比如阿拉伯数字、罗马数字等);\x0d\C:其他字符\x0d\\x0d\代码如下:\x0d\public class Test {\x0d\ public static void main(String[] args) {\x0d\ String str = "jioji,;'>:{}|+_`~47897423^&%(^1243-=kl;=-+_)j8932498";\x0d\ str = strreplaceAll("[\\pP‘’“”]", ",");\x0d\ Systemoutprintln(strsplit(",")length);\x0d\ }\x0d\}
你那个应该是哪个类里的一个字段吧。是一个常量吧。
Calendar中是这样的:
AM_PM:指示 HOUR 是在中午之前还是在中午之后。例如,在 10:04:15250 PM 这一时刻,AM_PM 为 PM。
HOUR:指示上午或下午的小时。HOUR 用于 12 小时制时钟 (0 - 11)。中午和午夜用 0 表示,不用 12 表示。例如,在 10:04:15250 PM 这一时刻,HOUR 为 10。
你那里的AM_PM=1 指的当前的时间是PM 即下午
HOUR=3 是指3点 也就是 是下午三点
以上就是关于java中,怎样把 20 Dec 2012 10am 这样的时间字符串转换成Date类型的要求的格式是1970-12-12 14:07:00全部的内容,包括:java中,怎样把 20 Dec 2012 10am 这样的时间字符串转换成Date类型的要求的格式是1970-12-12 14:07:00、java地图多个矩形计算亩数、JAVA,连接三个字符串:”I am”、” a student.”和“我是中国人。”等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)