据我所知,.是java正则表达式中的一个元字符。但是当我如下使用它时:
String s = "1.2.3.4";
Pattern pattern = Pattern.compile("[\\.]");
Run Code Online (Sandbox Code Playgroud)
我收到Redundant Character Escape来自 IntelliJ IDEA 的警告。有什么解释吗?
有很多实现如何检查String值是否为null,我真的很困惑使用什么!
这些是我到目前为止的搜索:
if(myString == null){}
Objects.equals(null, myString);
ObjectUtils.isEmpty(myString)
myString.equals(null)
myString.compareTo(null);
注意:这完全取决于性能,可靠编码,故障安全和其他安全目的!
更新: myString是动态的,如果它为null,上面的一些实现将抛出NullPointerException!
下面的行和注释行产生相同的结果:
public class StringEscapeMain {
public static void main(String[] args){
String fileName = "C:\\Dev\\TryEclipseJava\\StringEscape\\\\\\f.txt";
/* String fileName = "C:\\Dev\\TryEclipseJava\\StringEscape\\f.txt";*/
File file = new File(fileName);
if(file.exists()){
System.out.println("File exists!");
}
else{
System.out.println("File does not exist!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Java总是将任何超过2个斜杠的斜杠序列视为与"\"相同吗?
谢谢!
我有字符串类型的日期和时间20/03/2018, 18:20:44是否可以在java中将其更改为日期格式?我试过这段代码:
public static Date getDate(String dateString) {
DateFormat formatter = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("PST"));
try {
Date date = formatter.parse(dateString);
return date;
} catch (ParseException e) {
logger.error("error while parsing milliseconds to date" + dateString, e);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我无法解析异常并返回null
我刚刚安装了ta4j技术分析库.它有一个名为的接口类TimeSeries.当我尝试实现第一个方法时TimeSeries
String getName()
我收到以下错误:
不能从TimeSeries implaments org.ta4jcore.Timeserios.GetName中降低心灵变形的可靠性
我的守则
import org.ta4j.core.*;
public class cMyChartVal implements TimeSeries {
/**
* @return the name of the series
*/
String getName()
{
return "TestSet";
}
.....
.....
}
Run Code Online (Sandbox Code Playgroud)
TimeSeries 接口类
package org.ta4j.core;
import java.io.Serializable;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* Sequence of {@link Bar bars} separated by a predefined period (e.g. 15 minutes, 1 day, etc.)
* </p>
* Notably, a {@link TimeSeries time series} can be:
* <ul>
* <li>the …Run Code Online (Sandbox Code Playgroud) java.sql.Date date = java.sql.Date.valueOf("1900-01-01");
org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(date);
Run Code Online (Sandbox Code Playgroud)
基于上面的代码,我做了一些测试如下:
如果我将本地时区更改为UTC+8 Singapore Timezone,将会得到1899-12-31
如果我将本地时区更改为UTC+8 China Timezone,将会得到1900-01-01
如果我将本地时区更改为UTC-8 America/Los_Angeles Timezone,将会得到1900-01-01
如果我将本地时区更改为UTC+8 China Timezone,将会得到1900-01-01
有人可以帮忙澄清一下吗??它有点困惑。
BigDecimal x = new BigDecimal(1.95);
BigDecimal y = new BigDecimal(0.65);
BigDecimal rem = x.remainder(y);
if (rem.compareTo(BigDecimal.ZERO) != 0.) {
System.out.println("Not In mutilple of");
} else {
System.out.println("In mutilple of");
}
System.out.println(rem);
Run Code Online (Sandbox Code Playgroud)
没有给出上述场景的正确结果.给定输入条件但如果它是第二个值的倍数则给出不正确的结果
出于某些原因,我需要一个新对象来包装我从其他API获得的数据.
我遇到的问题是不知道从原始对象处理递归字段.
以下是示例代码:
//original object
public class Resource{
private String name;
private String content;
private Integer type;
private List<Resource> children = new LinkedList();
public void addChildrenResource(Resource children) {
this.children.add(children);
}
//getters&setters...
}
Run Code Online (Sandbox Code Playgroud)
//the object try to convert
public class Menu{
private String name;
private String url;
private Integer type;
private List<Menu> subMenu = new LinkedList();
public void addChildrenResource(Menu subMenu) {
this.children.add(subMenu);
}
//getters&setters...
}
Run Code Online (Sandbox Code Playgroud)
我做的实现,我不知道与递归字段...
这是我的代码..
List<Resource> resources = rpcService.getResources();
//Here I can't handle the subMenu or children part..
List<Menu> …Run Code Online (Sandbox Code Playgroud) 我在尝试解析日期字符串时看到以下错误,有人能指出我解析这个日期字符串的正确方向吗?
"2019-01-22T12:43:01Z"
Run Code Online (Sandbox Code Playgroud)
错误:
java.text.ParseException: Unparseable date: "2019-01-22T12:43:01Z"
Run Code Online (Sandbox Code Playgroud)
代码:
package ch02.ex1_1_HelloWorld
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.Date
import java.util.concurrent.TimeUnit
const val SERVER_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss:SS'Z'"
val sdf = SimpleDateFormat(SERVER_TIME_FORMAT, Locale.US)
fun main(args: Array<String>) {
timeSince("2019-01-22T12:43:01Z")
}
fun timeSince(dateStr: String) {
var diff : Long = 0
try {
diff = sdf.parse(dateStr).time - Date().time
} catch (e: Exception) {
print(e)
}
"${TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS)} h ago"
}
Run Code Online (Sandbox Code Playgroud) 我有一个转义的 json 字符串,例如
\n\n{\\"uid\\":\\"93\\",\\"channel_origin\\":\\"\\",\\"sys_ver\\":\\"12.2\\",\\"carrier_network_code\\":\\"02\\",\\"rpn\\":\\"\\\\u9996\\\\u9875\\",\\"app_ver\\":\\"9.3.8\\",\\"event_time\\":1556119062,\\"carrier\\":\\"\\\\u4e2d\\\\u56fd\\\\u79fb\\\\u52a8\\",\\"event_guid\\":\\"E36FE60BFBFF405680906E3F95EC3953\\",\\"ppn\\":\\"\\\\u542f\\\\u52a8\\",\\"device_token\\":\\"d323828f196a3ca665fa296a2a5f23a57cce83963328b73bd2cf78a7d55575ef\\",\\"day_first\\":\\"442\\",\\"abtest\\":\\"{\\\\\\"app_home_change\\\\\\":\\\\\\"b\\\\\\",\\\\\\"app_icon_change\\\\\\":\\\\\\"b\\\\\\"}\\",\\"pn\\":\\"\\\\u9996\\\\u9875\\",\\"puri\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/launch\\",\\"day_before\\":\\"1\\",\\"hardware_model\\":\\"iPhone10,3\\",\\"lng\\":\\"114.427362\\",\\"launch_guid\\":\\"69904D0912C943AC9D924AC8469CEA4E\\",\\"app_code\\":\\"cn.mafengwo.www\\",\\"_launch_source_history\\":\\"[]\\",\\"idfv\\":\\"20587F0C-0F6E-4CD2-94C3-CE3A10CF0EFD\\",\\"open_udid\\":\\"72F69B19-E612-4A4B-9D2F-8BB6C890421B\\",\\"carrier_country_code\\":\\"cn\\",\\"dev_ver\\":\\"D1908.0\\",\\"device_type\\":\\"ios\\",\\"ref\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"lat\\":\\"27.818787\\",\\"event_code\\":\\"show_index\\",\\"idfa\\":\\"DC7D35C1-C81B-4884-96C5-C6BAE83FA7DA\\",\\"uri\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"channel_code\\":\\"\\",\\"net\\":\\"1\\",\\"_is_foreground\\":\\"1\\",\\"sdk_ver\\":\\"20161011\\",\\"_event_hash\\":\\"8ae3b940026375b8252e689113fc8c43f5bfddcf\\",\\"ip\\":\\"39.176.24.64\\",\\"umddid\\":14696,\\"topic\\":\\"mobile_event\\",\\"ctime\\":1556119062,\\"ip_s\\":\\"node001026\\",\\"datetime\\":\\"2019-04-24T23:17:42+08:00\\",\\"uuid\\":\\"5c5ae87c-3b8b-2786-45b8-b16b969f8e7a\\",\\"hour\\":\\"23\\",\\"dt\\":\\"20190424\\",\\"minute\\":\\"17\\",\\"user_agent\\":\\"Safari Mozilla\\\\/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit\\\\/605.1.15 (KHTML, like Gecko) Mobile\\\\/15E148 mfwappcode\\\\/cn.mafengwo.www mfwappver\\\\/9.3.8 mfwappdevver\\\\/D1908.0 mfwjssdk\\\\/1.1 mfwappjsapi\\\\/1.7\\",\\"mfw_env\\":\\"product\\",\\"attr\\":{\\"_tpt\\":\\"\\\\u4fe1\\\\u606f\\\\u6d41\\",\\"prm_id\\":\\"\\",\\"_tpa\\":\\"\\\\u542f\\\\u52a8_\\\\u9996\\\\u9875\\",\\"item_name\\":\\"\\\\u673a\\\\u7968\\\\u706b\\\\u8f66\\\\u7968\\",\\"item_source\\":\\"\\",\\"_turi\\":\\"http:\\\\/\\\\/app.mafengwo.cn\\\\/index?banner=1&channel_id=55&theme=1\\",\\"_tp\\":\\"\\\\u9996\\\\u9875\\",\\"item_id\\":\\"\\",\\"item_uri\\":\\"http:\\\\/\\\\/m.mafengwo.cn\\\\/nb\\\\/public\\\\/sharejump.php?type=1500&source=app_home\\",\\"_tid\\":\\"B66086D2EA134BB3B7C957636E5F011C\\",\\"item_type\\":\\"\\",\\"_tpos\\":\\"index.index.index_icon.2\\",\\"show_cycle_id\\":\\"30261740-A01E-46D5-BBF7-24713BF03F69\\",\\"_tl\\":\\"1\\",\\"_tpre\\":\\"{\\\\\\"_turi\\\\\\":\\\\\\"http:\\\\\\\\\\\\/\\\\\\\\\\\\/app.mafengwo.cn\\\\\\\\\\\\/launch\\\\\\",\\\\\\"_tl\\\\\\":\\\\\\"0\\\\\\",\\\\\\"_tp\\\\\\":\\\\\\"\\\\u542f\\\\u52a8\\\\\\",\\\\\\"_tid\\\\\\":\\\\\\"C71450F83A6340A3BD9093E41D71FA42\\\\\\",\\\\\\"_tpi\\\\\\":\\\\\\"\\\\u542f\\\\u52a8\\\\\\",\\\\\\"_tpt\\\\\\":\\\\\\"\\\\u6b63\\\\u5e38\\\\\\"}\\",\\"pos_id\\":\\"index.index.index_icon.2\\",\\"_tpi\\":\\"40A0CE0E1CB243A399F70BEFDA8A5789\\",\\"module_name\\":\\"\\\\u997c\\\\u56fe\\",\\"sign_valid\\":1,\\"_client_upload_time\\":1556119062,\\"_dc_collect_audit_sdk_in\\":1556119062335,\\"_dc_collect_audit_sdk_out\\":1556119062347}}\nRun Code Online (Sandbox Code Playgroud)\n\n我可以在这个站点上取消转义它,它会生成有效的 json:
\n\n{\n "uid": "93",\n "channel_origin": "",\n "sys_ver": "12.2",\n "carrier_network_code": "02",\n "rpn": "\xe9\xa6\x96\xe9\xa1\xb5",\n "app_ver": "9.3.8",\n "event_time": 1556119062,\n "carrier": "\xe4\xb8\xad\xe5\x9b\xbd\xe7\xa7\xbb\xe5\x8a\xa8",\n "event_guid": "E36FE60BFBFF405680906E3F95EC3953",\n "ppn": "\xe5\x90\xaf\xe5\x8a\xa8",\n "device_token": "d323828f196a3ca665fa296a2a5f23a57cce83963328b73bd2cf78a7d55575ef",\n "day_first": "442",\n "abtest": "{\\"app_home_change\\":\\"b\\",\\"app_icon_change\\":\\"b\\"}",\n "pn": "\xe9\xa6\x96\xe9\xa1\xb5",\n "puri": "http://app.mafengwo.cn/launch",\n "day_before": "1",\n "hardware_model": "iPhone10,3",\n "lng": "114.427362",\n "launch_guid": "69904D0912C943AC9D924AC8469CEA4E",\n "app_code": "cn.mafengwo.www",\n "_launch_source_history": "[]",\n "idfv": "20587F0C-0F6E-4CD2-94C3-CE3A10CF0EFD",\n "open_udid": "72F69B19-E612-4A4B-9D2F-8BB6C890421B",\n "carrier_country_code": "cn",\n …Run Code Online (Sandbox Code Playgroud)