如何在片段中获取上下文?
我需要用我的数据库其构造函数采用的背景下,但getApplicationContext()并FragmentClass.this没有工作,所以我能做些什么?
数据库构造函数
public Database(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
Run Code Online (Sandbox Code Playgroud) 我想java.util.Collection根据谓词过滤一个.
将a转换Map<key,value>为a 的最佳方法是List<value>什么?只是迭代所有值并将它们插入列表中或者我忽略了什么?
我正在尝试在Android中创建邮件发送应用程序.
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
Run Code Online (Sandbox Code Playgroud)
这将启动内置的Android应用程序; 我正在尝试直接发送邮件,而不使用此应用程序.
在Java 8中,Stream.map()和Stream.flatMap()方法有什么区别?
在今天的工作中,我遇到了volatileJava中的关键字.我不太熟悉它,我发现了这个解释:
鉴于该文章解释了相关关键字的详细信息,您是否使用过它,或者您是否曾经看到过以正确方式使用此关键字的情况?
我试图将ISO 8601格式的字符串转换为java.util.Date.
yyyy-MM-dd'T'HH:mm:ssZ如果与Locale(比较样本)一起使用,我发现该模式符合ISO8601标准.
但是,使用java.text.SimpleDateFormat,我无法转换格式正确的String 2010-01-01T12:00:00+01:00.我必须先将它转换为2010-01-01T12:00:00+0100没有冒号的.
那么,目前的解决方案是
SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.GERMANY);
String date = "2010-01-01T12:00:00+01:00".replaceAll("\\+0([0-9]){1}\\:00", "+0$100");
System.out.println(ISO8601DATEFORMAT.parse(date));
Run Code Online (Sandbox Code Playgroud)
这显然不是那么好.我错过了什么或者有更好的解决方案吗?
回答
感谢JuanZe的评论,我发现了Joda-Time魔术,这里也有描述.
所以,解决方案是
DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
String jtdate = "2010-01-01T12:00:00+01:00";
System.out.println(parser2.parseDateTime(jtdate));
Run Code Online (Sandbox Code Playgroud)
或者更简单地说,通过构造函数使用默认解析器:
DateTime dt = new DateTime( "2010-01-01T12:00:00+01:00" ) ;
Run Code Online (Sandbox Code Playgroud)
对我来说,这很好.
我正在使用这些Scanner方法nextInt()并nextLine()阅读输入.
它看起来像这样:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
Run Code Online (Sandbox Code Playgroud)
问题是输入数值后,第一个input.nextLine()被跳过而第二个input.nextLine()被执行,所以我的输出如下所示:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and …Run Code Online (Sandbox Code Playgroud)